1、注意:web中OutputStream和Reader已经定义,如果再在方法入参里面定义就会报错
2、handler方法的返回值是String的时候,则跳转到指定视图页面,如果返回值是void而且调用Writer的write方法,就会在页面上输出对应的数据(该页面又SpringMVC创建)
3、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>SpringMVC_4_ServletAPI</display-name>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、springmvc.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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置自动扫描包 -->
<context:component-scan base-package="com.test.springmvc"></context:component-scan>
<!-- 配置视图解析器,把handler返回值解析为实际视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5、Handler.java
package com.test.springmvc.handlers;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/test")
@Controller
public class Handler {
//测试使用Servlet原生API作为参数
//注意:web中OutputStream和Reader已经定义,如果这里在定义就会报错
//这里如果返回result则跳到result.jsp,如果方法时void的而且调用Writer的write方法,就会在页面上输出对应的数据
@RequestMapping("/testServletAPI")
public void testServletAPI(HttpServletRequest request
,HttpServletResponse response
,HttpSession session
,Principal principal
,InputStream inputStream
//,OutputStream outputStream
,Writer writer
//,Reader reader
){
try{
System.out.println("HttpServletRequest:"+request);
System.out.println("HttpServletResponse:"+response);
System.out.println("HttpSession:"+session);
System.out.println("Principal:"+principal);
System.out.println("InputStream:"+inputStream);
//System.out.println("OutputStream:"+outputStream);
System.out.println("Writer:"+writer);
//System.out.println("Reader:"+reader);
writer.write("outputstream--outputstream");
}catch(Exception e){
e.printStackTrace();
}
//return "result";
}
}
6、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test/testServletAPI">testServletAPI</a>
</body>
</html>
7、result.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>
result page
</body>
</html>
8、项目目录
9、demo
https://download.csdn.net/download/qq_22778717/10599069