
正文
SpringMVC——注解的使用与结果跳转方式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.项目结构
2.源代码
package com.zhengbin.controller; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController {
@RequestMapping("/hello")
/* 1.设置modelandview对象
* 根据view的名称,和视图解析器跳转到指定的页面
* 页面:视图解析器的前缀+view name+视图解析器的后缀
* 需要在mvc.xml配置中实现视图解析器,才能实现
*/
public ModelAndView helloHandler(
HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
//封装要显示到视图中的数据
mav.addObject("msg", "hello springmvc by annotation");
//视图名
mav.setViewName("hello");//自动跳转至/WEB-INF/jsp/hello.jsp
return mav;
}
/*
* 2.通过Springmvc来实现转发和重定向,没有视图解析器
*/
@RequestMapping("/hello1")
public String hello1(){
//转发1
//return "index.jsp";
//转发2
//return "forward:index.jsp";
//重定向
return "redirect:index.jsp";
}
/*
* 3.通过ServletAPI对象来实现。不需要视图解析器的配置
*/
@RequestMapping("/hello2")
public void hello2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
//实现转发
request.setAttribute("msg", "hello2");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
@RequestMapping("/hello3")
public void hello3(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
//实现重定向
response.sendRedirect("index.jsp");
}
/*
* 4.使用SpringMVC,需要视图解析器
*/
@RequestMapping("/hello4")
public String hello4(){
//转发
return "hello";
//重定向不需要视图解析器
}
}
HelloController.java
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- 配置渲染器 视图解析器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 结果视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 结果视图的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 扫描该包中的所有注解 -->
<context:component-scan base-package="com.zhengbin.controller"/> </beans>
mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>01springmvc_hello</display-name> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<!--
1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
2)它的值必须是一个整数,表示servlet应该被载入的顺序
3)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
4)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
5)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
6)当值相同时,容器就会自己选择顺序来加载。
-->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</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>后







