@deftech2819 (kekekeke kekeke)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ラジオボタンで値を送信し更新をしたい

解決したいこと

ラジオボタンにて値を送信(性別)し,更新をする機能を実装しております。
性別を変更し更新ボタンを押しても,性別が変更されず戻ってしまいます。
解決方法を教えてください。

発生している問題・エラー

エラーメッセージ,コンソールエラーなどはありません。
上記のような問題が起きております。

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

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

1Answer

直接の原因は@kotazuckさんの指摘通り、th:valueによって現在設定されているユーザー情報のgenderが男女両方のラジオボタンの値に設定されているからですね。画面上でどちらを選択しても現在と同じ値がPOSTされます。

ただ、それ以外にもThymeleafとSpringWebの値のやり取り、テンプレートの記述法が理解できていない部分が多いようです。
Thymeleaf + Springfのチュートリアルに今一度目を通すのが良いかと思います。
ここでは使用されていないth:fieldや、逆に用法が間違っていると思われるth:each,th:switchなど、修正が必要な点が多いと思います。

1Like

Your answer might help someone💌