Spring MVC中用SimpleUrlHandlerMapping实现单纯的页面跳转
2017-07-31 18:16
579 查看
JavaEE开发中,为了安全,是不能直接访问JSP的,往往是通过一个Controller来访问一个JSP,但如果JSP多的话就要写很多个Controller,而它所实现的仅仅是页面跳转功能。所以如果访问的URL地址不涉及Service和Dao的话,只需要单纯的页面跳转的话,此时就可以通过SimpleUrlHandlerMapping实现页面的跳转功能,这时Controller就只充当一个将URL映射到内部JSP视图的转发器。
重点内容
但是要实现这一功能,必须要有一个限制,那就是将URL格式定死了,这样才能实现用一个Controller转发至多个不同的JSP的功能。
web.xml的内容:
其中,如果访问的URL匹配“/views/*.action”(这里就对格式进行了限制),那么就将其支配到BeanID对应的Controller去处理,这里是JspPageController.
在src目录下的cn.michael.controller包建一个名为JspPageController 的Controller,其内容为:
经过一系列方法链,从访问的URL剥离出对应的逻辑视图名jspfile,然后这个jspfile再经过视图解析器的解析就能够去访问内部的JSP等视图文件。
为了能够直接映射,这里我们直接用JSP的名字作为逻辑视图名。
在WEB-INF/pages/目录下建立一个testJspPage.jsp
然后浏览器中输入:http://localhost/SpringMVCDemo/views/testJspPage.action就能够访问到testJspPage.jsp.
项目地址:下载地址
重点内容
但是要实现这一功能,必须要有一个限制,那就是将URL格式定死了,这样才能实现用一个Controller转发至多个不同的JSP的功能。
web.xml的内容:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- SimpleUrlHandlerMapping 通过配置文件,把一个URL映射到Controller 配置Properties属性值 使用props和prop子节点来为Properties属性赋值 --> <bean id= "urlMapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <!-- prop标签中的key都是url值,后面的为BeanID,如果我们在地址栏中输入的url 与key匹配,则分发到prop标签中指定的beanID所指定的Controller --> <props> <prop key="/views/*.action">jspPageController</prop> </props> </property> <property name="interceptors"> <list> </list> </property> <property name= "order" value= "0" /> </bean> <!--id相当于定义了你的这个bean的别名,如果你需要他的话只要关联这个别名就可以了--> <bean id="jspPageController" class="cn.michael.controller.JspPageController"/> <!-- 配置处理器映射器和处理器适配器 使用<mvc:annotation-drvier/>配置注解映射器和注解适配器 --> <mvc:annotation-driven/> <!-- 配置处理器 使用<context:component-sacn/>组件扫描器自动扫描包中标记为@Controller的注解类, 注意:多个包中间使用半角逗号分隔 要求:base-package对应的包中应该是controller包 --> <context:component-scan base-package="cn.michael.controller"/> <!-- 配置视图解析器 要求: 1、配置解析JSP的视图解析器,默认使用JSTL,因此classpath下需要有JSTL的包 2、根据前缀和后缀,在WEB-INF目录下要有pages目录,其中存放jsp文件 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
其中,如果访问的URL匹配“/views/*.action”(这里就对格式进行了限制),那么就将其支配到BeanID对应的Controller去处理,这里是JspPageController.
<property name="mappings"> <!-- prop标签中的key都是url值,后面的为BeanID,如果我们在地址栏中输入的url 与key匹配,则分发到prop标签中指定的beanID所指定的Controller --> <props> <prop key="/views/*.action">jspPageController</prop> </props> </property>
在src目录下的cn.michael.controller包建一个名为JspPageController 的Controller,其内容为:
package cn.michael.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class JspPageController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse httpServletResponse) throws Exception { ModelAndView modelAndView = new ModelAndView(); String jspfile = request.getRequestURI() .substring(request.getContextPath().length()) .substring("views/".length()) .replace(".action",""); System.out.println ("JspPageController中的逻辑视图名"+jspfile); modelAndView.setViewName(jspfile); return modelAndView; } }
经过一系列方法链,从访问的URL剥离出对应的逻辑视图名jspfile,然后这个jspfile再经过视图解析器的解析就能够去访问内部的JSP等视图文件。
为了能够直接映射,这里我们直接用JSP的名字作为逻辑视图名。
在WEB-INF/pages/目录下建立一个testJspPage.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>测试SimpleURLMapping</title> </head> <body> <p> Test JspPage </p> </body> </html>
然后浏览器中输入:http://localhost/SpringMVCDemo/views/testJspPage.action就能够访问到testJspPage.jsp.
项目地址:下载地址
相关文章推荐
- Spring MVC 使用SimpleUrlHandlerMapping做映射处理的简单用例
- Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
- Springmvc借助SimpleUrlHandlerMapping实现接口开关功能
- SpringMVC基于非注解方式(SimpleUrlHandlerMapping)实现用户数据查询备忘(Intellij idea)
- Spring MVC: step by step 2-SimpleUrlHandlerMapping
- Spring MVC中使用SimpleUrlHandlerMapping映射处理器的一个简单例子
- Spring MVC-处理程序映射(Handler Mapping)-简单的Url处理程序映射(Simple Url Handler Mapping)示例(转载实践)
- Spring MVC的handlermapping之SimpleUrlHandlerMapping初始化
- spring MVC关于org.springframework.web.servlet.handler.SimpleUrlHandlerMapping的使用
- Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
- Spring MVC的handlermapping之请求分发如何找到正确的Handler(BeanNameUrlHandlerMapping,SimpleUrlHandlerMapping)
- spring mvc为何多注入了个SimpleUrlHandlerMapping?
- Error creating bean with name 'simpleUrlHandlerMapping' defined in ServletContext resource [/WEB-IN
- Spring SimpleUrlHandlerMapping的使用
- springMVC源码分析--SimpleUrlHandlerMapping(四)
- spring mvc注解方式实现向导式跳转页面
- ios系统中各种设置项的url链接 ,实现跳转系统页面设置
- 页面跳转传参:参数过长(cookie,url , AngularJs controller 传参实现)
- 本地html文件实现URL页面跳转
- 简单handler实现闪屏页面跳转