以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. sublime使用及插件

    转自 http://www.cnblogs.com/Rising/p/3741116.html

  2. JavaScript表示x的y次幂

    一.指数运算符(**) 示例 console.log(2 ** 2); // 4 console.log(3 ** 2); // 9 console.log('3' ** '2'); // 9 con ...

  3. Maven 整合SSH框架之pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. Java 常用工具类之 String 类

    String 类的特点: 字符串对象一旦被初始化就不会被改变. //以下代码的区别: String s = "abc"; // 在常量池中创建一个字符串对象, 池中没有就建立, 池 ...

  5. 初识idea

    http://blog.csdn.net/bitcarmanlee/article/details/54951589 http://blog.csdn.net/haishu_zheng/article ...

  6. Linux系统下实时监控网口速率的shell脚本

    修改后的脚本文件 #!/bin/bash #Modified by lifei4@datangmobile.cn echo ===DTmobile NetSpeedMonitor=== sleep 1 ...

  7. 从微观到宏观,遍历网络安全这幅有向图——By Me

    “可视化”是网络安全领域的前沿技术与可靠保障.笔者所在的西电捷通是一家领先的网络安全基础技术国际研究机构.一直从事网络安全基础技术研发与技术转移.笔者在2011年底入职典型技术之上的西电捷通公司,那时 ...

  8. 关于c#继承

    如下代码所示:最后输出的是:8,3,7,4 public class A { public virtual void One(int i) { Console.Write(i); } public v ...

  9. python 各种魔法方法

    目录 自定义序列 反射

  10. 聊天软件项目UDP升级版

    import java.net.*; import java.io.*; class UdpSend2 { public static void main(String[] args) throws ...