以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: 密码框的更多相关文章

  1. Spring MVC密码处理

    以下示例显示如何在使用Spring Web MVC框架的表单中使用密码.首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framework开发基于动态表单的 ...

  2. spring mvc:文本框

    采用:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> ...

  3. Spring MVC文本框

    以下示例显示如何使用Spring Web MVC框架在表单中使用文本框.首先使用Eclipse IDE来创建一个Web工程,按照以下步骤使用Spring Web Framework开发基于动态表单的W ...

  4. spring mvc:常用标签库(文本框,密码框,文本域,复选框,单选按钮,下拉框隐藏于,上传文件等)

    在jsp页面需要引入:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form&q ...

  5. spring mvc:复选框(多选)

    以user为例,user下有 username用户,password密码, address地址, receivePaper是否订阅, favotireFramework兴趣爱好, user.java ...

  6. Spring MVC列表多选框

    以下示例显示如何在使用Spring Web MVC框架的表单中使用列表框(Listbox).首先使用Eclipse IDE来创建一个WEB工程,实现一个让用户可选择自己所善长的技术(多选)的功能.并按 ...

  7. Spring MVC复选框(多项)

    以下示例显示如何在使用Spring Web MVC框架的表单中使用多个复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Frame ...

  8. Spring MVC复选框

    以下示例显示如何在使用Spring Web MVC框架的表单中使用复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...

  9. Spring MVC-表单(Form)标签-密码框(Password)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_password.htm 说明:示例基于Spring MVC 4.1.6. 以下示 ...

随机推荐

  1. [荐][转]为何应该使用 MacOS X(论GUI环境下开发人员对软件的配置与重用)

    一周前我和 Tinyfool 闲聊苹果操作系统,都认为对于开发人员来说,苹果操作系统(MacOS)是上佳的选择.Tinyfool 笔头很快,当即就写了一篇长文章,我则笔头很慢,今天才全部码好.他的文章 ...

  2. 字符串 (string)与字节数组(byte[])之间的转换

    string str = "abc" //字符串转成编码为GB2312的byte[] byte[] pData =System.Text.Encoding.GetEncoding( ...

  3. 洛谷 P4451 [国家集训队]整数的lqp拆分

    洛谷 这个题目是黑题,本来想打表的,但是表调不出来(我逊毙了)! 然后随便打了一个递推,凑出了样例, 竟然. 竟然.. 竟然... A了!!!!!!! 直接:\(f[i]=f[i-1]*2+f[i-2 ...

  4. 我的Android进阶之旅------>解决错误: java.util.regex.PatternSyntaxException: Incorrect Unicode property

    1.错误描述 今天使用正则表达式验证密码的时候,报了错误 java.util.regex.PatternSyntaxException: Incorrect Unicode property near ...

  5. Win7中的IIS配置asp时出现“出现403 文件夹禁止訪问错误”!

    Win7中的IIS配置asp时出现"出现403 文件夹禁止訪问错误"! 在[默认文档]中设一下启动文件即可了.

  6. 005-jdk安装卸载

    一.yum安装 1.查看CentOS自带JDK是否已安装. yum list installed |grep java 2.若有自带安装的JDK,卸载CentOS系统自带Java环境 卸载JDK相关文 ...

  7. 第一个Python程序(Day3)

    一    Python 解释器执行Python程序的过程   eg:python3 C:\ test.py 1.启动python解释器 (内存中) 2.将C:\ test.py中的内容从硬盘读入内存 ...

  8. 测试Windows live Writer

    private String GetRandomint(int codeCount) { Random random = new Random(); string min = "" ...

  9. jquery 获取checkbox 选中值并拼接字符集

    1.代码示例: var chk_value =[]; $('input[name="rewardids"]:checked').each(function(){   chk_val ...

  10. jsp页面向后台传递 不赋值 传递的默认值

    <input type="hidden" name="leaderIdentity" value="${subject.leaderId }&q ...