SpringMVC——注解的使用与结果跳转方式

1.项目结构

2.源代码

 1 package com.zhengbin.controller;
 2 
 3 
 4 import java.io.IOException;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.servlet.ModelAndView;
13 
14 @Controller
15 public class HelloController {
16     @RequestMapping("/hello")
17     /* 1.设置modelandview对象 
18      * 根据view的名称,和视图解析器跳转到指定的页面
19      * 页面:视图解析器的前缀+view name+视图解析器的后缀
20      * 需要在mvc.xml配置中实现视图解析器,才能实现
21      */
22     public ModelAndView helloHandler(
23             HttpServletRequest request, HttpServletResponse response) throws Exception {
24         ModelAndView mav = new ModelAndView();
25         //封装要显示到视图中的数据
26         mav.addObject("msg", "hello springmvc by annotation");
27         //视图名
28         mav.setViewName("hello");//自动跳转至/WEB-INF/jsp/hello.jsp
29         return mav;
30     }
31     /*
32      * 2.通过Springmvc来实现转发和重定向,没有视图解析器
33      */
34     @RequestMapping("/hello1")
35     public String hello1(){
36         //转发1
37         //return "index.jsp";
38         //转发2
39         //return "forward:index.jsp";
40         //重定向
41         return "redirect:index.jsp";
42     }
43     /*
44      * 3.通过ServletAPI对象来实现。不需要视图解析器的配置
45      */
46     @RequestMapping("/hello2")
47     public void hello2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
48         //实现转发
49         request.setAttribute("msg", "hello2");
50         request.getRequestDispatcher("index.jsp").forward(request, response);
51     }
52     @RequestMapping("/hello3")
53     public void hello3(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
54         //实现重定向
55         response.sendRedirect("index.jsp");
56     }
57     /*
58      * 4.使用SpringMVC,需要视图解析器
59      */
60     @RequestMapping("/hello4")
61     public String hello4(){
62         //转发
63         return "hello";
64         //重定向不需要视图解析器
65     }
66 }
HelloController.java
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd">
11 
12     <!-- 配置渲染器 视图解析器 -->
13     <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
15         <!-- 结果视图的前缀 -->
16         <property name="prefix" value="/WEB-INF/jsp/"/>
17         <!-- 结果视图的后缀 -->
18         <property name="suffix" value=".jsp"/>
19     </bean>
20     <!-- 扫描该包中的所有注解 -->
21     <context:component-scan base-package="com.zhengbin.controller"/>
22     
23 </beans>
mvc.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>01springmvc_hello</display-name>
 4   
 5   <servlet>
 6       <servlet-name>springmvc</servlet-name>
 7       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 8       <load-on-startup>1</load-on-startup>
 9       <init-param>
10           <param-name>contextConfigLocation</param-name>
11           <param-value>classpath:mvc.xml</param-value>
12       </init-param>
13       <!-- 
14           1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
15         2)它的值必须是一个整数,表示servlet应该被载入的顺序
16         3)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
17         4)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
18         5)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
19         6)当值相同时,容器就会自己选择顺序来加载。
20        -->
21   </servlet>
22   <servlet-mapping>
23       <servlet-name>springmvc</servlet-name>
24       <url-pattern>*.do</url-pattern>
25   </servlet-mapping>
26 </web-app>
web.xml

3.注意

  (1) 若不使用默认的路径与名称来创建springmvc配置文件,则需要在web.xml文件的DispatcherServlet中配置

  (2) 若使用注解来实现,则不需要在springmvc配置文件中声明handlerMapping与handlerAdapter

必须要声明<context:component-scan base-package=""/>来进行对该包中所有注解的扫描

  (3) 若在contorller中使用重定向则springmvc配置文件中的视图解析器不会起作用

  (4) 若在Eclipse下web.xml报cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'.错误

    则将<load-on-startup>1</load-on-startup>移至</init-param>

原文地址:https://www.cnblogs.com/zhengbin/p/5277229.html