ラジオボタンで値を送信し更新をしたい
解決したいこと
ラジオボタンにて値を送信(性別)し,更新をする機能を実装しております。
性別を変更し更新ボタンを押しても,性別が変更されず戻ってしまいます。
解決方法を教えてください。
発生している問題・エラー
エラーメッセージ,コンソールエラーなどはありません。
上記のような問題が起きております。
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
indexcontoller.java
package jp.co.sss.sys.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import jp.co.sss.sys.entity.Employee;
import jp.co.sss.sys.form.LoginForm;
import jp.co.sss.sys.repository.EmployeeRepository;
@Controller
@SessionAttributes(types = Employee.class)
public class IndexController {
@Autowired
EmployeeRepository empRepository;
LoginForm loginform;
/**
* ログイン画面を表示する
* @param loginForm
* @return login.html
*/
@RequestMapping(path = "/login", method = RequestMethod.GET)
public String login( LoginForm loginForm,BindingResult br,Model model) {
return "login";
}
@Autowired
HttpSession session;
// 処理
/**
* 入力された値を元にログイン認証し、トップ画面に遷移する
*
* @param req
* @param res
* @param loginForm
* @return top.html
*/
@RequestMapping(path = "/top", method = RequestMethod.POST)
public String login(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
//ログインした人の情報
String empId = req.getParameter("empId");
String password = req.getParameter("password");
Employee employee = empRepository.findByEmpIdAndPassword(empId, password);
//セッションデータ設定
session.setAttribute("userInfo",employee);
//ログインユーザー情報
model.addAttribute("employee",employee);
//ログインチェック
if(employee == null) {
//存在しない場合
return "login";
}else {
//存在した場合
//社員情報一覧
List<Employee> empAll= empRepository.findAll();
model.addAttribute("empAll",empAll);
return "top";
}
}
@RequestMapping(path = "/top", method = RequestMethod.GET)
public String top(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
List<Employee> empAll= empRepository.findAll();
model.addAttribute("empAll",empAll);
return "top";
}
@RequestMapping(path = "/mypage", method = RequestMethod.POST)
public String empUser(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) throws ParseException {
session = req.getSession();
String empName = req.getParameter("empName");
String password = req.getParameter("password");
String date = req.getParameter("birthday");
String savegender = req.getParameter("gender");
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdFormat.parse(date);
int gender = Integer.parseInt(savegender);
Employee userInfoUpdate = (Employee) session.getAttribute("userInfo");
userInfoUpdate.setEmpName(empName);
userInfoUpdate.setPassword(password);
userInfoUpdate.setBirthday(birthday);
userInfoUpdate.setGender(gender);
Employee updateEmployee = empRepository.save(userInfoUpdate);
return "edit_fin";
}
// TODO 自動生成されたメソッド・スタブ
//ログインユーザー情報
//マイページリンク押下,既存情報の出力
@RequestMapping(path = "/mypage", method = RequestMethod.GET)
public String empLink(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
session = req.getSession();
Object userInfo=session.getAttribute("userInfo");
model.addAttribute("userInfo",userInfo);
return "mypage";
}
@RequestMapping(path = "/edit_fin", method = RequestMethod.POST)
public String empBack(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
return "mypage";
}
}
mypage.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<link th:href="@{/css/style.css}" rel="stylesheet" />
<link th:href="@{/css/layout.css}" rel="stylesheet" />
<title>マイページ変更・確認画面</title>
</head>
<body>
<!-- ヘッダー -->
<header th:include="layout/header :: head">
</header>
<!-- サイドバー -->
<aside th:include="layout/aside :: side"></aside>
<!-- メイン -->
<article class="mypage">
<form action="" th:action="@{/mypage}" th:field="${userUpdate}"
method="post">
<h3 class="page_title">マイページ変更・確認画面</h3>
<table class="table employee">
<tr th:each="userInfo : ${userInfo}" th:object="${userInfo}">
<tr>
<th class="cell_title">社員番号</th>
<td th:text="*{userInfo.empId}"></td>
</tr>
<tr>
<th class="cell_title">名前</th>
<td><input type="text" name="empName"
th:value="${userInfo.empName}" /></td>
</tr>
<tr>
<th class="cell_title">パスワード</th>
<td><input type="password" name="password"
th:value="${userInfo.password}" /></td>
</tr>
<tr>
<th class="cell_title">生年月日</th>
<td><input type="text" name="birthday"
th:value="${#dates.format(userInfo.birthday,'yyyy-MM-dd')}" /></td>
</tr>
<tr>
<th class="cell_title">性別</th>
<td><input type="radio" name="gender"
th:value="${userInfo.gender}"
th:switch="*{userInfo.gender == 1}"
th:checked="${userInfo.gender == 1}" /> 男
<input type="radio" name="gender"
th:value="${userInfo.gender}"
th:switch="*{userInfo.gender == 2}"
th:checked="${userInfo.gender == 2}"> 女</td>
</tr>
</table>
<div class="btn_area_center">
<input type="submit" value="変更確定" class="btn">
</div>
</form>
</article>
<!-- フッダー -->
<footer th:include="layout/footer :: foot"></footer>
</body>
</html>
例)
def greet
puts Hello World
end
自分で試したこと
mypage.htmlの性別項目の書き方確認
value属性の追加記載
0 likes