springmvc mvc:view-controller使用

来源:互联网 发布:淘宝优惠群起什么名字 编辑:程序博客网 时间:2024/05/21 22:49

mvc:view-controller使用场景是在:一般springmvc都是经过Controller,但是当我们不想经过Controller,而是直接访问视图的时候。就可以通过mvc:view-controller。

如在WEB-INF中有个ta.jsp


<mvc:view-controller path="/" view-name="ta"/>

在spring-mvc.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"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       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-3.2.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <mvc:view-controller path="/" view-name="ta"/>    <mvc:annotation-driven  />    <!-- 自动扫描(自动注入) -->    <context:component-scan base-package="com.wwzuizz.**.controller"/>    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="2">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="contentType" value="text/html"/>        <property name="prefix" value="/WEB-INF/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

注意还要添加视图解析器InternalResourceViewResolver设置好jsp解析

这时候还要删除掉根目录下的index.jsp

因为默认jsp是交给tomcat/conf/web.xml中的 jsp servlet处理的

而在tomcat/conf/web.xml中有

 <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file>index.htm</welcome-file>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>

你可以去掉tomcat中web。xml里面的welcome-file-list中的index.jsp或者删除吊跟目绿的index.jsp

或者是直接spring拦截所有的jsp

然后运行访问/路径就是访问WEB-INF中的ta.jsp了

然后怎么设置访问到html的呢,如就想/访问的是ta.html

这时候因为DispatcherServlet是拦截所有请求,即便是静态文件Spring MVC会将它们当成一个普通请求处理,因此找不到对应处理器将导致错误。

有有两种方法

1、

    <mvc:default-servlet-handler/>

加上这句

2、

    <mvc:resources mapping="/WEB-INF/**" location="/WEB-INF/"/>
因为我的ta.html是在WEB-INF中的


最后修改下视图解析器

<?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"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       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-3.2.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <mvc:view-controller path="/" view-name="ta"/>    <mvc:annotation-driven  />    <!--<mvc:default-servlet-handler/>-->    <!-- 自动扫描(自动注入) -->    <context:component-scan base-package="com.wwzuizz.**.controller"/>    <mvc:resources mapping="/WEB-INF/**" location="/WEB-INF/"/>    <!--<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="2">-->        <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->        <!--<property name="contentType" value="text/html"/>-->        <!--<property name="prefix" value="/WEB-INF/"/>-->        <!--<property name="suffix" value=".jsp"/>-->    <!--</bean>-->    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver" >        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/"></property>        <property name="suffix" value=".html"/>        <property name="contentType" value="text/html"></property>    </bean></beans>

如上所示,当你直接运行的就是/就会访问到ta.html

重定向到控制器
<mvc:view-controller path="/" view-name="redirect:/admin/index"/>
即如果当前路径是/ 则重定向到/admin/index控制器中

单纯使用path

<mvc:view-controller path="/ta"/>

那么根据我们配置的页面解析器会自动访问的/ta的时候取访问/ta.html

参考:

http://www.cnblogs.com/dflmg/p/6393416.html

http://www.cnblogs.com/caoyc/p/5637894.html

http://blog.csdn.net/hong0220/article/details/39777689

原创粉丝点击