@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!

DBから取得する情報(1:男性,2:女性)によってラジオボタンに出力する初期値を変えたい

解決したいこと

前提:
DBに1:男性,2:女性(int型)としてデータが入っている
取得する情報によってラジオボタンに出力される初期値を変更したい。
解決方法をご教示いただければと思います。

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

ラジオボタン自体は出ているのですが,チェックがつかず出力されている

該当するソースコード

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 th:action="@{/mypage}" method="post" >


			<h3 class="page_title">マイページ変更・確認画面</h3>


			<table class="table employee">


				<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="1" th:checked="${gender == 1}" /> <label for="radioA">男性</label> 
						<input type="radio" name="gender"  
						th:value="2" /> <label for="radioB">女性</label>
				</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>
IndexController.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;
	HttpSession session;
	

	/**
	 * ログイン画面を表示する
	 * @param loginForm
	 * @return login.html
	 */
	@RequestMapping(path = "/login", method = RequestMethod.GET)
	public String login(LoginForm loginForm,BindingResult br,Model model) {
		return "login";
	}
	
	

	// 処理
	/**
	 * 入力された値を元にログイン認証し、トップ画面に遷移する
	 *
	 * @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) {
		session = req.getSession();
		List<Employee> empAll= empRepository.findAll();    
		model.addAttribute("empAll",empAll);

		return "top";

	}
	// 処理
		/**
		 * 入力された値を元に情報を更新し、更新完了画面に遷移する
		 *
		 * @param req
		 * @param res
		 * @param loginForm 
		 * @param editFin
		 * @return edit_fin.html
		 */



	@RequestMapping(path = "/mypage", method = RequestMethod.POST)
	public String empUser(@Validated  LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) throws ParseException   {
		

		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("employee");
		userInfoUpdate.setEmpName(empName);
		userInfoUpdate.setPassword(password);
		userInfoUpdate.setBirthday(birthday);
//		userInfoUpdate.setGender(gender);
		


		Employee updateEmployee = empRepository.save(userInfoUpdate);
		model.addAttribute("employee",updateEmployee);



		return "editFin";
	}

	// TODO 自動生成されたメソッド・スタブ

	@RequestMapping(path = "/mypage", method = RequestMethod.GET)
	public String empLink(LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
				
//		radioGender = initRadioGender();
//        model.addAttribute("radioGender", radioGender);
        
        
        
		session = req.getSession();
		Object userInfo=session.getAttribute("userInfo");

		model.addAttribute("userInfo",userInfo);
		return "mypage";
	}

	@RequestMapping(path ="/editFin", method = RequestMethod.POST)
	public String empBack(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
		session = req.getSession();
		
		
		

		
		return "editFin";
	}
	/**
	 * 更新完了画面を表示する
	 * @param editFin
	 * @return edit_fin.html
	 */
	@RequestMapping(path = "/editFin", method = RequestMethod.GET)
	public String edit(@Validated LoginForm loginForm, Employee editFin ,HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
		
		
		

		return "editFin";
	}
}

自分で試したこと

ここに問題・エラーに対して試したことを記載してください。
mypage.html内のchecked属性内の条件を変更
th:switchを使用するなど

0 likes

1Answer

ソースコードから察するにThymeleaf+Springだと思いますので、日本語版はないですがこちらのドキュメントを一通り目を通してみてはいかがでしょうか?
Springで利用する場合の追加機能について書かれています。

例えば、

<input type="text" name="empName" th:value="${userInfo.empName}" />

<form th:object="${userInfo}">
  <input type="text" th:field="*{empName}" />

といったようにシンプルに書けることがわかると思います。

ラジオボタンも同様に

<label><input type="radio" th:field="*{gender}" value="1" />男性</label> 
<label><input type="radio" th:field="*{gender}" value="2" />女性</label>

といったように書けることがわかると思います。

1Like

Your answer might help someone💌