SpringMVC学习笔记(六)

1、Controller方法的返回类型

(1)ModelAndView

需要在方法结束时定义ModelAndView并设置model和view,然后返回ModelAndView对象。

(2)String

表示方法返回逻辑视图名,真正视图=前缀+逻辑视图名+后缀。

(3)void

方法的参数包含请求对象和响应对象。

2、实例

(1)项目结构


(2)在web.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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC22</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置前端控制器 -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

(3)在spring-servlet.xml文件中配置处理器映射器、处理器适配器、视图解析器和Handler处理器

<?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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <context:component-scan base-package="com.hello.controller" />
    
    <mvc:annotation-driven />
    
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路径的前缀 -->
		<property name="prefix" value="/WEB-INF/page/" />
		<!-- 配置jsp路径的后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

(4)编写Handler处理器HelloController

@Controller
@RequestMapping("/hello")
public class HelloController {

	@RequestMapping("/hello1.action")
	public ModelAndView hello1() throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "Hello1");
		modelAndView.setViewName("hello");
		return modelAndView;
	}
	
	@RequestMapping("/hello2.action")
	public String hello2(Model model) throws Exception {
		
		model.addAttribute("message", "Hello2");
		return "hello";//返回逻辑视图名
	}
	
	@RequestMapping("/hello3.action")
	public String hello3() throws Exception {
		
		return "redirect:hello1.action";//重定向(request不可以共享)
	}
	
	@RequestMapping("/hello4.action")
	public String hello4() throws Exception {
		
		return "forward:hello1.action";//转发(request可以共享)
	}
	
	@RequestMapping("/hello5.action")
	public void hello5(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.sendRedirect("hello1.action");//重定向
	}
	
	@RequestMapping("/hello6.action")
	public void hello6(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.getRequestDispatcher("hello1.action").forward(request, response);//转发
	}
	
	@RequestMapping("/hello7.action")
	public void hello7(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write("json串");
	}
}

(5)hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	${message }
</body>
</html>
(6)结果

http://localhost:8080/SpringMVC22/hello/hello1.action


http://localhost:8080/SpringMVC22/hello/hello2.action


http://localhost:8080/SpringMVC22/hello/hello3.action


http://localhost:8080/SpringMVC22/hello/hello4.action


http://localhost:8080/SpringMVC22/hello/hello5.action


http://localhost:8080/SpringMVC22/hello/hello6.action


http://localhost:8080/SpringMVC22/hello/hello7.action


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值