1. 程式人生 > >spring mvc: 密碼框

spring mvc: 密碼框

表單 sharp highlight str ise ati NPU java ans

以user為例,包含username, password字段.

user.java

public class User {

   private String username;
   private String password;

   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
}

  

UserController.java代碼

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class UserController {

   @RequestMapping(value = "/user", method = RequestMethod.GET)
   public ModelAndView user() {
      return new ModelAndView("user_index", "command", new User());
   }

   @RequestMapping(value = "/addUser", method = RequestMethod.POST)
   public String addUser(@ModelAttribute("SpringWeb")User user, 
      ModelMap model) {
      model.addAttribute("username", user.getUsername());
      model.addAttribute("password", user.getPassword());

      return "user_add";
   }
}

  

這裏的第一個服務方法user(),我們已經在ModelAndView對象中傳遞了一個名稱為“command”的空User對象,因為如果在JSP文件中使用<form:form>標簽,spring框架需要一個名稱為“command”的對象。 所以當調用user()方法時,它返回user.jsp視圖。

第二個服務方法addUser()將根據URL => HelloWeb/addUser 上的POST方法請求時調用。根據提交的信息準備模型對象。 最後從服務方法返回“userlist”視圖,這將呈現userlist.jsp視圖。

user_index.jsp 的代碼如下所示

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>添加user信息</title>
</head>
<body>

<form:form method="post" action="/hello/addUser">
<table>
	<tr>
		<td><form:label path="username">姓名:</form:label></td>
		<td><form:input path="username" /></td>
	</tr>
	<tr>
		<td><form:label path="password">密碼</form:label></td>
		<td><form:password path="password"/></td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="submit" value="提交表單"/>
		</td>
	</tr>
</table>
</form:form>

</body>
</html>

  user_add.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用戶信息顯示</title>
</head>
<body>


<table>
	<tr>
		<td>用戶名</td>
		<td>${username}</td>
	</tr>
	<tr>
		<td>密碼</td>
		<td>${password}</td>
	</tr>
</table>


</body>
</html>

  

如圖:

技術分享圖片

技術分享圖片

spring mvc: 密碼框