首页 > 编程 > JSP > 正文

SpringMVC学习笔记(四)使用JSP处理表单(处理用户注册表单)

2019-11-11 03:30:33
字体:
来源:转载
供稿:网友

参考

(1)《SPRing入门经典》(基于这本书第三章)


步骤

(1)根据原型maven-archetype-webapp创建一个空的Maven Web应用程序项目。向pom.xml文件中添加spring-webmvc及其子项目依赖项,这里spring的版本为4.1.7.RELEASE

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zqx</groupId> <artifactId>SpringMVCFormelements</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>SpringMVCFormelements Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version> </dependency> </dependencies> <build> <finalName>SpringMVCFormelements</finalName> </build></project>

(2)在web.xml文件中通过URL映射定义Dispatecher Servlet:

web.xml:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>

(3)为应用程序上下文配置创建springmvc-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.wiley.beginningspring.ch3" /> <context:annotation-config /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean></beans>

(4)创建UserController类:

package com.wiley.beginningspring.ch3.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.wiley.beginningspring.ch3.domain.Gender;import com.wiley.beginningspring.ch3.domain.User;@Controllerpublic class UserController { private static final String[] countries = { "Turkey", "United States", "Germany", "China"}; @RequestMapping(value = "/form") public ModelAndView user() { ModelAndView modelAndView = new ModelAndView("userForm", "user", new User()); modelAndView.addObject("genders", Gender.values()); modelAndView.addObject("countries", countries); return modelAndView; } @RequestMapping(value = "/result") public ModelAndView processUser(User user) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("userResult"); modelAndView.addObject("u", user); return modelAndView; }}

(5)在/WEB-INF/pages文件夹下创建userForm.jsp页面:

<%@ page contentType="text/html; charset=ISO-8859-1" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc" %><html><head> <title>Spring MVC Form Handling</title></head><body><h2>User Registration Form</h2><mvc:form modelAttribute="user" action="result.mvc"> <table> <tr> <td><mvc:label path="name">Name</mvc:label></td> <td><mvc:input path="name" /></td> </tr> <tr> <td><mvc:label path="lastname">Last Name</mvc:label></td> <td><mvc:input path="lastname" /></td> </tr> <tr> <td><mvc:label path="passWord">Password</mvc:label></td> <td><mvc:password path="password" /></td> </tr> <tr> <td><mvc:label path="detail">Detail</mvc:label></td> <td><mvc:textarea path="detail" /></td> </tr> <tr> <td><mvc:label path="birthDate">Birth Date</mvc:label></td> <td><mvc:input path="birthDate" /></td> </tr> <tr> <td><mvc:label path="gender">Gender</mvc:label></td> <td><mvc:radiobuttons path="gender" items="${genders}" /></td> </tr> <tr> <td><mvc:label path="country">Country</mvc:label></td> <td><mvc:select path="country" items="${countries}" /></td> </tr> <tr> <td><mvc:label path="nonSmoking">Non Smoking</mvc:label></td> <td><mvc:checkbox path="nonSmoking" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit" /> </td> </tr> </table></mvc:form></body></html>

(6)在/WEB-INF/pages文件夹下创建userResult.jsp页面:

<%@ page contentType="text/html; charset=ISO-8859-1" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc" %><html><head> <title>Spring MVC Form Handling</title></head><body><h2>User Registration Result</h2><table> <tr> <td>Name</td> <td>${u.name}</td> </tr> <tr> <td>Last name</td> <td>${u.lastname}</td> </tr> <tr> <td>Password</td> <td>${u.password}</td> </tr> <tr> <td>Detail</td> <td>${u.detail}</td> </tr> <tr> <td>Birth Date</td> <td>${u.birthDate}</td> </tr> <tr> <td>Gender</td> <td>${u.gender}</td> </tr> <tr> <td>Country</td> <td>${u.country}</td> </tr> <tr> <td>Non-Smoking</td> <td>${u.nonSmoking}</td> </tr></table></body></html>

(7)代码结构

这里写图片描述

(8)测试

这里写图片描述 这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表