基础环境搭建请参考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");
}
}

最新文章

  1. Jser 设计模式系列之面向对象 - 接口封装与继承
  2. 数据库一些常用的SQL语句
  3. ae柱状图
  4. 对C++虚函数、虚函数表的简单理解
  5. valueOf和toString
  6. 发起post请求
  7. text-align:-moz-center与text-align:-webkit-center区别与用法
  8. https 方式使用git@osc设置密码的方式
  9. IE 8兼容:X-UA-Compatible的解释
  10. 初探 FFT/DFT
  11. Canvas rontate(旋转) 使用误区
  12. Linux网络管理常用命令:net-tools VS iproute2
  13. Chapter 4. Working with Key/Value Pairs
  14. 一道风骚的DP
  15. 计算机基础-Day1
  16. org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be res
  17. Go 语言函数
  18. 4.BN推导
  19. mysql 报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executin
  20. 网络编程-Mysql-2、各种查询

热门文章

  1. 用python的BeautifulSoup分析html
  2. ASP.NET Core Authentication and Authorization
  3. 用序列到序列和注意模型实现的:Translation with a Sequence to Sequence Network and Attention
  4. Matlab GUI设计(2)
  5. CSS3+CSS+HTML实现网页
  6. js函数简单调用
  7. 【WPF学习】第六十四章 构建基本的用户控件
  8. 第一章构建vue项目,代码仓库管理
  9. 使用systemctl工具
  10. HDFS程序开发