第二天,看到第三章SpringMVC的常用注解了,接下来一起学SpringMVC的常用注解吧
首先,创建一个新的项目
选maven,然后next
第一栏填包名,第二栏写项目名,具体的命名规则参考
Java包(package)的命名规范,java中package命名规则
然后next
然后点finish
然后配置web,点左上角File--->Project Structure...
配置
然后点OK
现在文件目录如下所示:
maven导包:修改pom.xml,内容如下
<!--这个参数用于统一配备Spring的版本-->
<properties>
<spring.version>4.2.0.RELEASE</spring.version>
</properties>
<!--需要用到的编译插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!--依赖-->
<dependencies>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument-tomcat -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
在web.xml中添加servlet,添加内容如下:
<servlet>
<!--Servlet的名称-->
<servlet-name>dispatcher</servlet-name>
<!--Servlet对应的java类-->
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!--web应用启动时立即加载Servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--Servlet的映射声明-->
<servlet-mapping>
<!--对应Servlet的名称-->
<servlet-name>dispatcher</servlet-name>
<!--标识监听当前的所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
接着HelloController.java文件
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
return "hello";
}
}
最后在WEB-INF的中添加dispatcher-servlet配置文件:
dispatcher-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"
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">
<!--Spring可以自动扫描base-package下面的子包或者子包下面的java文件,将于Spring注解相关的类注册为Spring的Bean-->
<context:component-scan base-package="pers.congcong.controller"/>
<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix">
<value>/WEB-INF/jsp</value>
</property>
<!--后缀-->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
然后创建一个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>Hello World啦啦啦哎哎哎</title>
</head>
<body>
哎哎哎哎哎
</body>
</html>
最后配置服务器
然后点Apply
然后运行
翻车了
jsp后面少了一个/,给他加上
修改dispatcher-servlet.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<!--后缀-->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
改回来,接着运行。
然后是一段话,SpringMVC中用于参数绑定的注解有很多,都在org.springframework.web.bind.annotation包中,根据它们处理request的不同可以分为四类(主要讲解常用类型):
处理request body部分的注解:
@RequestParam
@RequestBody
处理requet uri部分的注解
@PathVariable
处理request header部分的注解
@RequestHeader
@CookieValue
处理attribute类型的注解
@SessionAttribute
@ModelAttribute
一脸懵逼,这些注解就一个单词解决,就不能走点心吗!
接下来讲RequestMapping的属性:
value属性
用来映射一个请求或一种方法
@RequestMapping(value = "/parame" )
public ModelAndView hello(){
return ...;
}
或者
@RequestMapping("parame")
public String hello(){
return "hello";
}
mothod属性:
用来指示该方法仅仅处理哪些HTTP请求
RequestMethod.POST
...GET
...HEAD
...OPTIONS
...PUT
...PATCH
...DELETE
...TRACE
等方法
consume属性,指定处理请求的提交内容类型
@RequestMapping(value = "/parame", method = RequestMethod.POST, consumes = "application/json")
表示该方法仅用于处理request Content-Type为“application/json”类型的请求
produces属性
该属性指定返回的内容类型,返回的内容类型必须是request请求头(Accept)中所包含的类型
@RequestMapping(value = "/parame", method = RequestMethod.POST, produces = "application/json")
public String hello(){
return "hello";
}
表示该方法仅用于处理request Access头中包含“application/json”的请求,同时指明返回的内容类型为“application/json”
params属性
@RequestMapping(value = "/parame", params = "myParam = myValue")
public String hello(){
return "hello";
}
仅处理符合要求的请求
header属性
@RequestMapping(value = "/parame",method = RequestMethod.POST, headers = "Referer = http://www.congcong.com")
表示仅处理header中包含指定的“Referer”请求头和对应的值“http://www.congcong.com”的请求
请求处理方法可出现的参数类型:
- Javax.servlet.ServletRequest或Javax.servlet.http.HttpServletRequest
- Javax.servlet.ServletResponse或Javax.servlet.http.HttpServletResponse
- javax.servlet.http.HttpSession
- org.springframework.web.context.request.WebRequest
或org.springframework.web.context.request.NativeWebRequest - java.util.Locale
- java.io.InputStream 或java.io.Reader
- java.io.OutputStream或 java.io.Writer
- java.security.Principal
- HttpEntity<?>
- java.util.Map
- org.springframework.ui.Model
- org.springframework.ui.ModelMap
- org.springframework.ui.Model
- org.springframework.web.servlet.mvc.support.RedierctAttributes
- org.springframework.validation.Errors
- org.springframework.validation.BindingResult
- org.springframework.web.bind.support.SessionStatus
- org.springframework.web.util.UriComponentsBuilder
- @PathVariable、@MarixVariable
- @ReqeustParam @RequestHeader @ReqesetBody @RequestPart注解
请求可返回的参数类型
- org.springframework.web.portlet.ModelAndView
- org.springframework.ui.Model
- java.util.Map<k,v>
- org.springframework.web.servlet.View
- java.lang.String
- HttpEntity或ResponseEntity
- java.util.concurrent.Callable
- org.springframework.web.context.request.async.DeferredResult
- void