1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Java,SpringBoot】顧客情報登録webアプリを作る

Last updated at Posted at 2023-07-18

使用ツール
SpringBoot

codeダウンロード
https://drive.google.com/drive/folders/1h4nyNx4SeFfiDmiSB5xPv1Y1aTSYycML?usp=sharing

Page1
登録情報入力
2.png

Page2
登録情報の登録
3.png

Page3
データベースに出力
5.png

package com.example.demo;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Register {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/")
    public String start() {
        return "input.html";
    }

    @RequestMapping("/register")
    public ModelAndView register(@ModelAttribute RegisterBean rb, ModelAndView m) {
        User user = new User();
        user.setName(rb.getName());
        user.setAge(rb.getAge());
        userRepository.save(user); // データベースへの保存

        m.addObject("rb", rb);
        m.setViewName("register.html");
        return m;
    }
    
    @RequestMapping("/data")
    public ModelAndView showData(ModelAndView m) {
        List<User> users = userRepository.findAll();
        m.addObject("users", users);
        m.setViewName("data.html");
        return m;
    }
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>入力画面</title>
</head>
<body>
  <h1>登録情報を入力してください</h1>
  <form action="/register" method="post">
  名前:<input type="text" name="name" size="10" /><br/>
  年齢:<input type="radio" name="age" value="18歳未満"/>18歳未満 
        <input type="radio" name="age" value="18歳以上"/>18歳以上<br/><br/>
  <input type="submit" value="送信" />
  </form>
</body>
</html>

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?