SpringMVC转发及重定向
2024-09-07 10:05:05
基础环境搭建请参考SringMVC入门程序
1:springmvc-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.applesnt.controller"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler />
<!--annotation-driven配置帮助我们完成处理器映射器和处理器适配器-->
<mvc:annotation-driven />
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2:controller
com\applesnt\controller\HelloController.java
package com.applesnt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class HelloController {
/*走视图解析器转发*/
@RequestMapping("/t1")
public String t1(HttpServletRequest request, HttpServletResponse response, Model model){
HttpSession session = request.getSession();
model.addAttribute("msg","走视图解析器 sessionID = "+session.getId());
//无视图解析器 就要写全路径
return "hello";
}
/*不走视图解析器转发 关键字:forward*/
@RequestMapping("/t5")
public String t5(HttpServletRequest request, HttpServletResponse response, Model model){
HttpSession session = request.getSession();
model.addAttribute("msg","不走视图解析器 sessionID = "+session.getId());
//无视图解析器 就要写全路径
return "forward:/WEB-INF/jsp/hello.jsp";
}
/*不走视图解析器重定向 关键字:redirect*/
@RequestMapping("/t6")
public String t6(HttpServletRequest request, HttpServletResponse response, Model model){
//无视图解析器 就要写全路径
return "redirect:/index.jsp";
}
/*serlvet方式转发 */
@GetMapping("/t2")
public void t2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
request.setAttribute("msg","serlvet方式转发 sessionID = "+session.getId());
request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request,response);
}
/*servlet方式输出*/
@GetMapping("/t3")
public void t3(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
response.getWriter().print("servlet方式输出 sessionID = "+session.getId());
}
/*servlet方式重定向*/
@GetMapping("/t4")
public void t4(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/index.jsp");
}
}
最新文章
- Jser 设计模式系列之面向对象 - 接口封装与继承
- 数据库一些常用的SQL语句
- ae柱状图
- 对C++虚函数、虚函数表的简单理解
- valueOf和toString
- 发起post请求
- text-align:-moz-center与text-align:-webkit-center区别与用法
- https 方式使用git@osc设置密码的方式
- IE 8兼容:X-UA-Compatible的解释
- 初探 FFT/DFT
- Canvas rontate(旋转) 使用误区
- Linux网络管理常用命令:net-tools VS iproute2
- Chapter 4. Working with Key/Value Pairs
- 一道风骚的DP
- 计算机基础-Day1
- org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be res
- Go 语言函数
- 4.BN推导
- mysql 报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executin
- 网络编程-Mysql-2、各种查询