Spring mvcフロントバックグラウンドデータインタラクション

4599 ワード

1.パラメータ対応
JAva側:
 
	@RequestMapping(value="/test",method = RequestMethod.POST)
	public String submit(String name,Model model){
		//String name = request.getParameter("name");
        mailClient.send("[email protected]", name);		
		model.addAttribute("username",name);
		return "account/testhome";
	}

    HTML:
 
   
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:social="http://spring.io/springsocial"
	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	  layout:decorator="layout">
	<head>
		<title>    </title>
	</head>
	<body>
		<div id="content" layout:fragment="content">
			<form action="#" method="POST" th:action="@{/app/account/test}">
			<p>
				   :<input name="name" type="text"></input>
			</p>
			<p>
				<button type="submit">  </button>
			</p>
			
			</form>
		</div>	
	</body>
</html>

 2.HttpServeretRequest処理パラメータ
JAva側:
 
 
@RequestMapping(value="/test",method = RequestMethod.POST)
	public String submit(HttpServletRequest request){
		String name = request.getParameter("name");
		return "account/testhome";
	}

 
    HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:social="http://spring.io/springsocial"
	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	  layout:decorator="layout">
	<head>
		<title>    </title>
	</head>
	<body>
		<div id="content" layout:fragment="content">
			<form action="#" method="POST" th:action="@{/app/account/test}">
			<p>
				   :<input name="name" type="text"></input>
			</p>
			<p>
				<button type="submit">  </button>
			</p>
			
			</form>
		</div>	
	</body>
</html>

 
3.thymeleaf対応
	@RequestMapping(value="/register", method=RequestMethod.POST)
	public String RegisterFormSubmit(@ModelAttribute CredentialUser user,Model model){
		register.registerAccount(user.getUsername(),"", "");
		auth.register(user.getUsername(), user.getPassword());
		model.addAttribute("user", user);
		return "account/home";
	}

 
    HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:social="http://spring.io/springsocial"
	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	  layout:decorator="layout">
	<head>
		<title>    </title>
	</head>
	<body>
		<div id="content" layout:fragment="content">
			<form action="#" method="POST" th:action="@{/app/account/register}" th:object="${user}">
			<p>
				   :<input th:field="*{username}" type="text"></input>
			</p>
			<p>
				  :<input th:field="*{password}" type="password"></input>
			</p>
			<p>
			</p>
<!-- 			<p> -->
<!-- 				   :<input th:field="verifyCode" type="text"></input><img alt="   " src=""></img> -->
<!-- 			</p> -->
			<p>
				<button type="submit">  </button>
			</p>
			
			
			</form>
		</div>	
	</body>
</html>