SpringMVC_04重定向和转发

在配置视图解析器的情况下

<!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>


    <bean name="/t1" class="controller.ControllerTest1"/>

转发正常写就行

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class ModelTest1 {

    @RequestMapping("/m1/t1")
    public String test(HttpServletRequest request, HttpServletResponse response)
    {
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        return "test";
    }
}

重定向只需要加上:redirect

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class ModelTest1 {

    @RequestMapping("/m1/t1")
    public String test(HttpServletRequest request, HttpServletResponse response)
    {
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        return "redirect:test";
    }
}

 

上一篇:如何在具有空布局的 JPanel 中添加 JTable?


下一篇:java springboot实现图片上传服务器