基于annotation注解的Spring3 MVC Form Handling Example之二表单提交学习笔记
2013-12-20 20:49
706 查看
基于annotation注解的Spring3 MVC Form Handling Example之二表单提交学习笔记
一,项目结构图:
com.jiangge.common.controller.Student
com.jiangge.common.controller.StudentController
/Spring3MVCDemoForm/WebRoot/WEB-INF/jsp/result.jsp
/Spring3MVCDemoForm/WebRoot/WEB-INF/jsp/student.jsp
/Spring3MVCDemoForm/WebRoot/WEB-INF/HelloWeb-servlet.xml
/Spring3MVCDemoForm/WebRoot/WEB-INF/web.xml
http://localhost:8080/Spring3MVCDemo/student
点击提交后:
http://localhost:8080/Spring3MVCDemo/addStudent
参考:
http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm
一,项目结构图:
com.jiangge.common.controller.Student
package com.jiangge.common.controller; public class Student { private Integer age; private String name; private Integer id; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
com.jiangge.common.controller.StudentController
package com.jiangge.common.controller; 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; /* * Here the first service method student(), * we have passed a blank Student object in the ModelAndView object with name "command" * because the spring framework expects an object with name "command" if you are using <form:form> tags in your JSP file. * So when student() method is called it returns student.jsp view */ @Controller public class StudentController { @RequestMapping(value = "/student", method = RequestMethod.GET) public ModelAndView student() { return new ModelAndView("student", "command", new Student());//返回student.jsp视图 } /* * addStudent() will be called against a POST method on the /addStudent URL. * You will prepare your model object based on the submitted information. * Finally a "result" view will be returned from the service method, which will result in rendering result.jsp */ @RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model) { //提交过来的数据 model.addAttribute("name", student.getName()); model.addAttribute("age", student.getAge()); model.addAttribute("id", student.getId()); return "result"; //返回result.jsp视图 } }
/Spring3MVCDemoForm/WebRoot/WEB-INF/jsp/result.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>Spring MVC 表单处理</title> </head> <body> <h2>Submitted Student Information</h2> <table> <tr> <td>名字:</td> <td>${name}</td> </tr> <tr> <td>年龄:</td> <td>${age}</td> </tr> <tr> <td>ID:</td> <td>${id}</td> </tr> </table> </body> </html>
/Spring3MVCDemoForm/WebRoot/WEB-INF/jsp/student.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>Spring MVC表单处理</title> </head> <body> <h2>学生信息</h2> <form:form method="POST" action="/addStudent"> <table> <tr> <td><form:label path="name">名字</form:label></td> <td><form:input path="name" /></td> </tr> <tr> <td><form:label path="age">年龄</form:label></td> <td><form:input path="age" /></td> </tr> <tr> <td><form:label path="id">id</form:label></td> <td><form:input path="id" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> </td> </tr> </table> </form:form> </body> </html>
/Spring3MVCDemoForm/WebRoot/WEB-INF/HelloWeb-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:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.jiangge.common.controller" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前缀后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
/Spring3MVCDemoForm/WebRoot/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Form Application</display-name> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码过滤器解决中文乱码,强制编码格式为UTF-8 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>注:url 为 /*
http://localhost:8080/Spring3MVCDemo/student
点击提交后:
http://localhost:8080/Spring3MVCDemo/addStudent
参考:
http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm
相关文章推荐
- 基于annotation注解的Spring3 MVC Page Redirection Example之三页面重定向学习笔记
- Java 学习笔记06:Spring 基于注解(Annotation)的AOP
- Java 学习笔记05:Spring 基于注解(Annotation)的IOC
- 基于注解的spring3.0.x MVC学习笔记(二)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(八)
- spring注解基于Annotation的依赖注入配置笔记
- Spring4学习笔记-AOP(基于注解的方式)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(五)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(六)
- Spring 2.5.6.SEC01 基于注解的表单提交、验证框架、 View端采用模板语言Jangod
- Spring学习笔记-springMVC基于注解的控制器(基本概念)
- spring学习笔记 -- day14 基于注解的整合
- 基于注解的spring3.0.x MVC学习笔记
- java事务学习笔记(七)--像Spring一样使用Transactional注解(Annotation)
- Spring学习总结5(IOC-基于注解Annotation)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(四)
- Spring Boot 学习笔记【7】FreeMarker提交表单
- Spring学习笔记(12.基于java的容器注解)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(三)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(二)