springmvc多个视图解析器管理跳转资源

springmvc多个视图解析器管理跳转资源

项目结构
在这里插入图片描述

第一步:将所有资源交由springmvc管理

 <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
</servlet>
<servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

在springmvc配置文件中配置多个视图解析器

根据属性viewNames的value进行区分,*jsp*:处理器方法返回的资源包括jsp的则进来
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
         <!--基于上下文进行对组件的扫描-->
    <context:component-scan base-package="com.yyjc.springmvc.handler"/>
	
    <mvc:annotation-driven/>
    <!--使用默认的Servlet来响应静态文件-->
    <mvc:default-servlet-handler/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
        <property name="viewNames" value="*jsp*"/>
        <property name="order" value="2"/>
    </bean>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".html"/>
        <property name="viewNames" value="i*"/>
        <property name="order" value="1"/><!--order:设置优先级,order值越低,则优先级越高-->
    </bean>
</beans>

第二步:处理器方法返回跳转资源

注:方法返回结果可以是ModelAndView或者String

@RequestMapping("/userlogin.action")
    public String userLogin() {
        System.out.println("进来了");
        return "jsp/success";
	}
/*
@RequestMapping("/userlogin.action")
    public ModelAndView userLogin()  {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("进来了");
        modelAndView.setViewName("index");
        return modelAndView;
 	}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值