springmvc mvc:view-controller使用

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值