根据springMVC学习总结(一) --springMVC搭建搭建项目

  1. 在com.myl.controller包下创建一个java类WebController。
  2. 在jsp子文件夹下创建一个视图文件index.jsp、final.jsp。
  3. 配置web.xml
  4. 配置springmvc-servler.xml

创建项目结构如下:

WebController.java代码如下

package com.myl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
*
* @author myl
* @date 2018年5月20日 上午11:13:39
*/ @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";
}
}

下面是Spring视图文件index.jsp的内容。用登录页面演示,该页面发送访问重定向方法的请求,将重定向这个请求到另一个服务方法,最后将享受final.jsp页面的内容

index.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>form演示重定向</title>
</head>
<body>
<p>spring mvc重定向页面</p>
<h2>点击下面按钮提交表单,跳转重定向界面</h2>
<form:form method="GET" action="/springmvc_03/redirect">
<table>
<tr>
<td>
<input type="submit" value="提交重定向">
</td>
</tr>
</table>
</form:form>
</body>
</html>

final.jsp代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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重定向页面</title>
</head>
<body>
<h2>final重定向页面</h2>
</body>
</html>

web.xml 配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     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>springmvc_03</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>

springmvc-servlet.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.myl.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

运行该项目

访问http://localhost:8080/springmvc_03/index结果如下

点击  提交重定向按钮结果如下:

到此简单的重定向代码完成

最新文章

  1. csharp: .NET Object Relationional Mapper (ORM)- SubSonic
  2. 关于line-height
  3. matlab初学之plot颜色和线型
  4. java实现吸血鬼数字
  5. PowerDesigner英文字段转换中文字段显示
  6. hdu2717 Catch That Cow
  7. Codeforces AIM Tech Round3
  8. c++中vector使用
  9. Python爬虫小白---(二)爬虫基础--Selenium PhantomJS
  10. gstreamer在Ubuntu下构建开发环境
  11. CouchDB客户端开发—Java版
  12. latex中使用定理、证明、缩进
  13. python 小程序,猜年龄
  14. 使用脚本与orm模型交互对数据库操作
  15. Confluence 6 删除一个空间
  16. L300 3月英语课下
  17. Django-url反向解析和命名空间
  18. React 与 Redux 在生产环境中的实践总结
  19. 查看Linux内核及发行商版本命令
  20. Linux学习4-远程登录管理工具安装

热门文章

  1. 本地图片转base64编码
  2. Android 开发必备的知识点——JVM基础【转】
  3. 当鼠标移入div上时,div的背景色在4s之内渐变为灰色,同时在5s之内顺时针旋转45度,且尺寸缩小一半;当鼠标移走时,再渐变恢复初始样式
  4. Mac搭建Vue开发环境
  5. Azure安装完postgresql遇到:psql: error: could not connect to server: FATAL: no pg_hba.conf entry for host
  6. SPOJ ABCDEF题解
  7. CCS box-flex属性
  8. javaScript学习关于常用注册监听和对象的创建
  9. Spring Cloud Alibaba - Spring Cloud Stream 整合 RocketMQ
  10. JavaScript学习03(函数)