spring mvc helloworld 和表单功能、页面重定向
Spring MVC Hello World 例子
这里有个很好的教程:https://www.cnblogs.com/wormday/p/8435617.html
下面的例子说明了如何使用 Spring MVC 框架来编写一个简单的基于 web 的 Hello World 应用程序。下面让我们使用 ideal IDE,然后按照下面的步骤使用 Spring 的 Web 框架来开发一个动态 Web 应用程序:
这里是 HelloController.java 文件的内容:
- package com.tutorialspoint;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.ui.ModelMap;
- @Controller
- @RequestMapping("/hello")
- public class HelloController{
- @RequestMapping(method = RequestMethod.GET)
- public String printHello(ModelMap model) {
- model.addAttribute("message", "Hello Spring MVC Framework!");
- return "hello";
- }
- }
下面是 Spring Web 配置文件 web.xml 的内容
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <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>
- </web-app>
下面是另一个 Spring Web 配置文件 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:context="http://www.springframework.org/schema/context"
- 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">
- <context:component-scan base-package="com.rxhui.springmvc"/>
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
下面是 Spring 视图文件 hello.jsp 的内容,注意:1.dispatch-servlet.xml中前缀是/jsp,所以应该把hello.jsp放入一个jsp文件夹中。
- <%@ page contentType="text/html; charset=UTF-8" %>
- <html>
- <head>
- <title>Hello World</title>
- </head>
- <body>
- <h2>${message}</h2>
- </body>
- </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 文件的内容:
- public class Student {
- private Integer age;
- private String name;
- private Integer id;
- public void setAge(Integer age) {
- this.age = age;
- }
- public Integer getAge() {
- return age;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public Integer getId() {
- return id;
- }
- }
下面是 StudentController.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 StudentController {
- @RequestMapping(value = "/student", method = RequestMethod.GET)
- public ModelAndView student() {
- return new ModelAndView("student", "command", new Student());
- }
- @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";
- }
- }
在这里,第一个 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 的内容
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <display-name>Spring MVC Form Handling</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <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>
- </web-app>
下面是另一个 Spring Web 配置文件 HelloWeb-servlet.xml 的内容
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="com.tutorialspoint" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
下面是 Spring 视图文件 student.jsp 的内容
- <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
- <html>
- <head>
- <title>Spring MVC Form Handling</title>
- </head>
- <body>
- <h2>Student Information</h2>
- <jsp:useBean id="command" class="com.xxx.xx.Student" scope="request"/>
- <form:form method="POST" action="/addStudent">
- <table>
- <tr>
- <td><form:label path="name">Name</form:label></td>
- <td><form:input path="name" /></td>
- </tr>
- <tr>
- <td><form:label path="age">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="Submit"/>
- </td>
- </tr>
- </table>
- </form:form>
- </body>
- </html>
- <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 的内容
- <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
- <html>
- <head>
- <title>Spring MVC Form Handling</title>
- </head>
- <body>
- <h2>Submitted Student Information</h2>
- <table>
- <tr>
- <td>Name</td>
- <td>${name}</td>
- </tr>
- <tr>
- <td>Age</td>
- <td>${age}</td>
- </tr>
- <tr>
- <td>ID</td>
- <td>${id}</td>
- </tr>
- </table>
- </body>
- </html>
Spring 页面重定向例子
这里只用WebController的例子说明一下
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- @Controller
- public class WebController {
- @RequestMapping(value = "/index", method = RequestMethod.GET)
- public String index() {
- return "index";
- }
- @RequestMapping(value = "/redirect", method = RequestMethod.GET)
- public String redirect() {
- return "redirect:finalPage";
- }
- @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
- public String finalPage() {
- return "final";
- }
- }
- 这里实际的流程是访问/index页面,点击按钮进行重定向,发送执行/redirect,“redirect:finalPage"中,redirect是个关键词,就是重定向url的意思,冒号后面的finalPage是urlGet请求,最终会返回final.jsp
spring mvc helloworld 和表单功能、页面重定向的更多相关文章
- Spring MVC(十)--通过表单序列化传递参数
通过表单序列化传递参数就是将表单数据转化成字符串传递到后台,序列化之后参数请求变成这种模式param1=value1&¶m2=value2,下面用代码实现. 1.创建表单 &l ...
- Spring MVC实现防止表单重复提交(转)
Spring MVC拦截器+注解方式实现防止表单重复提交
- Spring mvc 简单异常配置jsp页面
原文出处:http://howtodoinjava.com/spring/spring-mvc/spring-mvc-simplemappingexceptionresolver-example/ 这 ...
- IDEA+Maven+Spring MVC HelloWorld示例
用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件 ...
- Spring mvc 加载HTML静态页面
看到网上大部分举例Spring MVC加载静态页面HTML方式都还要通过controller, 根据js和css文件的加载模式,html也同样可以直接加载 在spring的配置文件中例如 *-serv ...
- 关于Spring MVC中的表单标签库的使用
普通的MVC设计模式中M代表模型层,V代表视图层,C代表控制器,SpringMVC是一个典型的MVC设置模式的框架,对于视图和控制器的优化很多,其中就有与控制器相结合的JSP的表单标签库. 我们先简单 ...
- Spring MVC helloWorld中遇到的问题及解决办法
1.java.io.FileNotFoundException: Could not open ServletContext resource不能加载ServletContext的用法是配置到web. ...
- Spring MVC HelloWorld入门及运行机制 (一)
完整的项目案例: springmvc.zip 介绍 SpringMVC是一款Web MVC框架. 它跟Struts框架类似,是目前主流的Web MVC框架之一. 文章通过实例来介绍SpringMVC的 ...
- spring mvc:练习:表单验证(javaConfig配置和注解)
使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS,JavaScript,图片) ...
随机推荐
- 【Demo】CSS3 2D转换
2D转换transform 2D变换方法: translate() 根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动. div { transform: translate(50px,1 ...
- Python 用Redis简单实现分布式爬虫
Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台. 连接数据库 注意:假设现有几台在同一局域网内的机器分别为Master和几个Slaver Master连接 ...
- java 多媒体发送邮件
import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mai ...
- asp.net获取URL和IP地址
(转自:http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html) HttpContext.Current.Request.Ur ...
- Fiddler工作原理与代理设置
1,什么是Fiddler Fiddler是一个http协议调试代理工具,它能够记录客户端和服务器之间的所有 HTTP请求,可以针对特定的HTTP请求,分析请求数据.设置断点.调试web应用.修改请求的 ...
- git远程分支回退
[本地代码回退] git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除 git reset --hard HEAD~3:将 ...
- css 中 overflow: hidden清楚浮动的真正原因
1. 先上一段代码清楚浮动的代码, 外层ul设置overflow为hidden, 内层li设置float为left左浮动 <!DOCTYPE html> <html> < ...
- 学习nodejs部分基础内容入门小结
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node.js 的包管理器 n ...
- canvas 创建渐变图形
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Windows上包管理器之Chocolatey初体验
一直使用Windows开发项目,前段时间使用了一段时间的macOS,感觉使用homebrew和npm去安装一些常用的包真的是方便啊,最近又使用回Windows,由于电脑比较新,发现里面连Git都没有, ...