SpringMVC通过Controller实现页面跳转

有些时候我们需要跳转到WEB-INF目录下的页面,这个时候直接通过超链接的形式是不可以直接跳转的,必须要经过后端来进行跳转 。

首先要在spring配置文件中做如下配置:

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    

在Controller中

@RequestMapping("/exit")
    public String out(HttpServletRequest request, HttpServletResponse response){
        return "index";

    }

这样就可以跳转到WEB-INF/jsp/index.jsp页面了

另附项目中的一份spring-*.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" 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/context http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
                    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <context:component-scan base-package="com.common,com.XXXX,com.codegen" />
    
    <mvc:annotation-driven />
    
    <mvc:resources mapping="/resources/**" location="/resources/" />
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    bean>

    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    <bean id="namedJdbcTemplate"
        class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg>
            <ref bean="dataSource" />
        constructor-arg>
    bean>
    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />
    
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp" />
    
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxInMemorySize" value="10240" /> 
        <property name="maxUploadSize" value="-1" /> 
    bean>
    
    <bean id="SpringContextUtil" class="com.common.util.SpringContextUtil" />
    
    <aop:aspectj-autoproxy />
beans>

你可能感兴趣的:(SpringMVC)