课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring MVC 表单处理例子

当前位置:免费教程 » Java相关 » Spring

下面的例子说明了如何编写一个简单的基于 web 的应用程序,它利用了使用 Spring 的 Web MVC 框架的 HTML 表单。为了开始使用它,让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤使用 Spring 的 Web 框架来开发一个动态的基于表单的 Web 应用程序:

步骤 描述
1 创建一个名称为 HelloWeb动态 Web 项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint
2 将上面提到的 Spring 和其他库拖拽到文件夹 WebContent/WEB-INF/lib 中。
3 com.tutorialspoint 包下创建一个 Java 类 StudentStudentController
4 WebContent/WEB-INF 文件夹下创建 Spring 的配置文件 Web.xmlHelloWeb-servlet.xml
5 WebContent/WEB-INF 文件夹下创建名称为 jsp 的子文件夹。在这个子文件夹下创建视图文件 student.jspresult.jsp
6 最后一步是创建所有的源代码和配置文件的内容,并导出该应用程序,正如下面解释的一样。

这里是 Student.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class Student {
  3. private Integer age;
  4. private String name;
  5. private Integer id;
  6. public void setAge(Integer age) {
  7. this.age = age;
  8. }
  9. public Integer getAge() {
  10. return age;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21. public Integer getId() {
  22. return id;
  23. }
  24. }

下面是 StudentController.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.ModelAttribute;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.ui.ModelMap;
  8. @Controller
  9. public class StudentController {
  10. @RequestMapping(value = "/student", method = RequestMethod.GET)
  11. public ModelAndView student() {
  12. return new ModelAndView("student", "command", new Student());
  13. }
  14. @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
  15. public String addStudent(@ModelAttribute("SpringWeb")Student student,
  16. ModelMap model) {
  17. model.addAttribute("name", student.getName());
  18. model.addAttribute("age", student.getAge());
  19. model.addAttribute("id", student.getId());
  20. return "result";
  21. }
  22. }

在这里,第一个 service 方法 student(),我们已经在名称为 “command” 的 ModelAndView 对象中传递一个空的 Student 对象,因为 spring 框架需要一个名称的 “command” 的对象,如果你在 JSP 文件中使用 <form:form> 标签。所以,当 student() 方法被调用时,它返回 student.jsp 视图。

第二个 service 方法 addStudent() 将调用 HelloWeb/addStudent URL 中的 POST 方法。你将根据提交的信息准备好你的模型对象。最后一个 “result” 视图会从 service 方法中返回,它将导致呈现 result.jsp。

下面是 Spring Web 配置文件 web.xml 的内容

  1. <web-app id="WebApp_ID" version="2.4"
  2. xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6.  
  7. <display-name>Spring MVC Form Handling</display-name>
  8.  
  9. <servlet>
  10. <servlet-name>HelloWeb</servlet-name>
  11. <servlet-class>
  12. org.springframework.web.servlet.DispatcherServlet
  13. </servlet-class>
  14. <load-on-startup>1</load-on-startup>
  15. </servlet>
  16.  
  17. <servlet-mapping>
  18. <servlet-name>HelloWeb</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21.  
  22. </web-app>

下面是另一个 Spring Web 配置文件 HelloWeb-servlet.xml 的内容

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9.  
  10. <context:component-scan base-package="com.tutorialspoint" />
  11.  
  12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13. <property name="prefix" value="/WEB-INF/jsp/" />
  14. <property name="suffix" value=".jsp" />
  15. </bean>
  16.  
  17. </beans>

下面是 Spring 视图文件 student.jsp 的内容

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring MVC Form Handling</title>
  5. </head>
  6. <body>
  7.  
  8. <h2>Student Information</h2>
  9. <form:form method="POST" action="/HelloWeb/addStudent">
  10. <table>
  11. <tr>
  12. <td><form:label path="name">Name</form:label></td>
  13. <td><form:input path="name" /></td>
  14. </tr>
  15. <tr>
  16. <td><form:label path="age">Age</form:label></td>
  17. <td><form:input path="age" /></td>
  18. </tr>
  19. <tr>
  20. <td><form:label path="id">id</form:label></td>
  21. <td><form:input path="id" /></td>
  22. </tr>
  23. <tr>
  24. <td colspan="2">
  25. <input type="submit" value="Submit"/>
  26. </td>
  27. </tr>
  28. </table>
  29. </form:form>
  30. </body>
  31. </html>

下面是 Spring 视图文件 result.jsp 的内容

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring MVC Form Handling</title>
  5. </head>
  6. <body>
  7.  
  8. <h2>Submitted Student Information</h2>
  9. <table>
  10. <tr>
  11. <td>Name</td>
  12. <td>${name}</td>
  13. </tr>
  14. <tr>
  15. <td>Age</td>
  16. <td>${age}</td>
  17. </tr>
  18. <tr>
  19. <td>ID</td>
  20. <td>${id}</td>
  21. </tr>
  22. </table>
  23. </body>
  24. </html>

最后,下面是包含在你的 web 应用程序中的 Spring 和其他库的列表。你仅仅需要将这些文件拖拽到 WebContent/WEB-INF/lib 文件夹中。

  • commons-logging-x.y.z.jar

  • org.springframework.asm-x.y.z.jar

  • org.springframework.beans-x.y.z.jar

  • org.springframework.context-x.y.z.jar

  • org.springframework.core-x.y.z.jar

  • org.springframework.expression-x.y.z.jar

  • org.springframework.web.servlet-x.y.z.jar

  • org.springframework.web-x.y.z.jar

  • spring-web.jar

一旦你完成了创建源代码和配置文件后,导出你的应用程序。右键单击你的应用程序,并且使用 Export > WAR File 选项,并且在 Tomcat 的 webapps 文件夹中保存你的 HelloWeb.war 文件。

现在启动你的 Tomcat 服务器,并且确保你能够使用标准的浏览器访问 webapps 文件夹中的其他 web 页面。现在尝试访问该 URL http://localhost:8080/SpringWeb/student。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:

在提交必需的信息之后,单击提交按钮来提交这个表单。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号