Spring MVC Hello World 例子

这里有个很好的教程:https://www.cnblogs.com/wormday/p/8435617.html

下面的例子说明了如何使用 Spring MVC 框架来编写一个简单的基于 web 的 Hello World 应用程序。下面让我们使用 ideal IDE,然后按照下面的步骤使用 Spring 的 Web 框架来开发一个动态 Web 应用程序:

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

  1. package com.tutorialspoint;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.ui.ModelMap;
  6. @Controller
  7. @RequestMapping("/hello")
  8. public class HelloController{
  9. @RequestMapping(method = RequestMethod.GET)
  10. public String printHello(ModelMap model) {
  11. model.addAttribute("message", "Hello Spring MVC Framework!");
  12. return "hello";
  13. }
  14. }

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>/WEB-INF/applicationContext.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <servlet>
  14. <servlet-name>HelloWeb</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>HelloWeb</servlet-name>
  20. <url-pattern>/</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

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

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

下面是 Spring 视图文件 hello.jsp 的内容,注意:1.dispatch-servlet.xml中前缀是/jsp,所以应该把hello.jsp放入一个jsp文件夹中。

  1. <%@ page contentType="text/html; charset=UTF-8" %>
  2. <html>
  3. <head>
  4. <title>Hello World</title>
  5. </head>
  6. <body>
  7. <h2>${message}</h2>
  8. </body>
  9. </html>

使用Ideal导出war:project structure->Artifacts->+->WebApplication:Exploded-> WebApplication:Archive-> 改名->save->build-> build Artifacts -> 刚才的Archive->build

现在启动你的 Tomcat 服务器,并且确保你能够使用标准的浏览器访问 webapps 文件夹中的其他 web 页面。现在尝试访问该 URL http://localhost:8080/HelloWeb/hello。如果你的 Spring Web 应用程序一切都正常,就可以看到页面,注意,这里访问的url中HelloWeb是webapps下包的名字,如果使用IDE直接调试Tomcat,不需要带HelloWeb。

Spring MVC 表单处理例子

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

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

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

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

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

注意:上面的command是ModelAndView默认的名称,但是如果想要正常运行,还需要在jsp中注入一个bean.

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>/WEB-INF/applicationContext.xml</param-value>
  9. </context-param>
  10.  
  11. <display-name>Spring MVC Form Handling</display-name>
  12.  
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>/WEB-INF/applicationContext.xml</param-value>
  16. </context-param>
  17. <listener>
  18. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  19. </listener>
  20. <servlet>
  21. <servlet-name>HelloWeb</servlet-name>
  22. <servlet-class>
  23. org.springframework.web.servlet.DispatcherServlet
  24. </servlet-class>
  25. <load-on-startup>1</load-on-startup>
  26. </servlet>
  27.  
  28. <servlet-mapping>
  29. <servlet-name>HelloWeb</servlet-name>
  30. <url-pattern>/</url-pattern>
  31. </servlet-mapping>
  32. </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. <jsp:useBean id="command" class="com.xxx.xx.Student" scope="request"/>
  10. <form:form method="POST" action="/addStudent">
  11. <table>
  12. <tr>
  13. <td><form:label path="name">Name</form:label></td>
  14. <td><form:input path="name" /></td>
  15. </tr>
  16. <tr>
  17. <td><form:label path="age">Age</form:label></td>
  18. <td><form:input path="age" /></td>
  19. </tr>
  20. <tr>
  21. <td><form:label path="id">id</form:label></td>
  22. <td><form:input path="id" /></td>
  23. </tr>
  24. <tr>
  25. <td colspan="2">
  26. <input type="submit" value="Submit"/>
  27. </td>
  28. </tr>
  29. </table>
  30. </form:form>
  31. </body>
  32. </html>
  1. <jsp:useBean id="command" class="com.xxx.xx.Student" scope="request"/>这句会向form中注入一个student,否则会抛出Neither BindingResult nor plain target object for bean name 'command' available as request attribute

下面是 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>

Spring 页面重定向例子

这里只用WebController的例子说明一下

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. @Controller
  5. public class WebController {
  6. @RequestMapping(value = "/index", method = RequestMethod.GET)
  7. public String index() {
  8. return "index";
  9. }
  10. @RequestMapping(value = "/redirect", method = RequestMethod.GET)
  11. public String redirect() {
  12. return "redirect:finalPage";
  13. }
  14. @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
  15. public String finalPage() {
  16. return "final";
  17. }
  18. }
  1. 这里实际的流程是访问/index页面,点击按钮进行重定向,发送执行/redirect,“redirect:finalPage"中,redirect是个关键词,就是重定向url的意思,冒号后面的finalPage是urlGet请求,最终会返回final.jsp

spring mvc helloworld 和表单功能、页面重定向的更多相关文章

  1. Spring MVC(十)--通过表单序列化传递参数

    通过表单序列化传递参数就是将表单数据转化成字符串传递到后台,序列化之后参数请求变成这种模式param1=value1&&param2=value2,下面用代码实现. 1.创建表单 &l ...

  2. Spring MVC实现防止表单重复提交(转)

    Spring MVC拦截器+注解方式实现防止表单重复提交  

  3. Spring mvc 简单异常配置jsp页面

    原文出处:http://howtodoinjava.com/spring/spring-mvc/spring-mvc-simplemappingexceptionresolver-example/ 这 ...

  4. IDEA+Maven+Spring MVC HelloWorld示例

    用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件 ...

  5. Spring mvc 加载HTML静态页面

    看到网上大部分举例Spring MVC加载静态页面HTML方式都还要通过controller, 根据js和css文件的加载模式,html也同样可以直接加载 在spring的配置文件中例如 *-serv ...

  6. 关于Spring MVC中的表单标签库的使用

    普通的MVC设计模式中M代表模型层,V代表视图层,C代表控制器,SpringMVC是一个典型的MVC设置模式的框架,对于视图和控制器的优化很多,其中就有与控制器相结合的JSP的表单标签库. 我们先简单 ...

  7. Spring MVC helloWorld中遇到的问题及解决办法

    1.java.io.FileNotFoundException: Could not open ServletContext resource不能加载ServletContext的用法是配置到web. ...

  8. Spring MVC HelloWorld入门及运行机制 (一)

    完整的项目案例: springmvc.zip 介绍 SpringMVC是一款Web MVC框架. 它跟Struts框架类似,是目前主流的Web MVC框架之一. 文章通过实例来介绍SpringMVC的 ...

  9. spring mvc:练习:表单验证(javaConfig配置和注解)

    使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS,JavaScript,图片) ...

随机推荐

  1. 【Demo】CSS3 2D转换

    2D转换transform 2D变换方法: translate() 根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动. div { transform: translate(50px,1 ...

  2. Python 用Redis简单实现分布式爬虫

    Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台. 连接数据库 注意:假设现有几台在同一局域网内的机器分别为Master和几个Slaver Master连接 ...

  3. java 多媒体发送邮件

    import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mai ...

  4. asp.net获取URL和IP地址

    (转自:http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html) HttpContext.Current.Request.Ur ...

  5. Fiddler工作原理与代理设置

    1,什么是Fiddler Fiddler是一个http协议调试代理工具,它能够记录客户端和服务器之间的所有 HTTP请求,可以针对特定的HTTP请求,分析请求数据.设置断点.调试web应用.修改请求的 ...

  6. git远程分支回退

    [本地代码回退] git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除 git reset --hard HEAD~3:将 ...

  7. css 中 overflow: hidden清楚浮动的真正原因

    1. 先上一段代码清楚浮动的代码, 外层ul设置overflow为hidden, 内层li设置float为left左浮动 <!DOCTYPE html> <html> < ...

  8. 学习nodejs部分基础内容入门小结

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node.js 的包管理器 n ...

  9. canvas 创建渐变图形

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. Windows上包管理器之Chocolatey初体验

    一直使用Windows开发项目,前段时间使用了一段时间的macOS,感觉使用homebrew和npm去安装一些常用的包真的是方便啊,最近又使用回Windows,由于电脑比较新,发现里面连Git都没有, ...