spring mvc之三:注解开发

前言

之前我们都是通过xml方式显示配置,会发现配置起来特别麻烦。当使用注解时,有时只需在相关类中做相关的声明,就能完成同样的工作。

不多说:先来一个注解开发例子

步骤一:首先创建一个web工程

步骤二:添加jar包

步骤三:配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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">
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载方式
默认加载必须规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
-->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

步骤四:配置springmvc-servlet.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 扫描基本的父包,这样子类包被自动扫到执行 -->
<context:component-scan base-package="com.zwl"></context:component-scan>
<!-- 配置注解处理器映射器
功能:寻找执行类Controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解处理器适配器
功能:调用controller方法,执行controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
</bean>
<!-- 配置sprigmvc视图解析器:解析逻辑试图
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

这里简要说明一下,以上是注解最基本的配置,有扫描配置,两个基本配置和一个视图配置

步骤五:自定义controller类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.zwl.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //相当于<bean class="Usercontrol"/>并且声明这是一个controller
public class Usercontrol {
@RequestMapping("hello") //相当于需要访问的路径hello.do
public String say(){
return "index" ; //放回到逻辑视图index.jsp
}
}

这是一个最简化的注解配置
对应index.jsp

1
2
3
<body>
hello 盲僧
</body>

测试:浏览器中输入http://localhost:100/springmvc2/hello.do

对应输出结果

hello 盲僧

RequestMapping

它有三种方式,分别为

requestMapping(“hello”)
requestMapping(“/hello.do”)
requestMapping(value=”/hello.do”)

都能达到同样的效果,都是需要访问的路径
如果在类前面添加此属性,则相当于映射路径在加一层

1
2
3
@Controller //<bean class="Usercontrol"/>
@RequestMapping("/test")
public class Usercontrol {}

http://localhost:100/springmvc2/test/hello.do

另外,注意下里面还有个method配置,即按什么模式接受表单

@RequestMapping(value=”/hello.do”,method=RequestMethod.GET)

或者

@RequestMapping(value=”/hello.do”,method=RequestMethod.POST)

默认是两个都可以的

requestMapping(value=”/hello.do”,method={RequestMethod.POST, RequestMethod.GET})

封装参数

分析接受参数类型:
基本类型,int,String等等基本类型。
Pojo类型
包装类型
Springmvc参数封装:基于方法进行封装。

一:基本类型
封装int型参数
页面

1
2
3
4
5
6
<body>
<form action="${pageContext.request.contextPath }/ test/recieve.do" method="post">
id:<input type="text" name="id" id="id">
<input type="submit" value="提交">
</form>
</body>

接受参数方法

1
2
3
4
5
@RequestMapping("recieve")
public String recieve(Integer id){
System.out.println(id);
return "login";
}

注意这里的Integer id必须要与页面中的name=”id”一致才能正确接受参数

测试:在浏览器中输入http://localhost:100/springmvc2/index.jsp

然后输入id,后台即可接收到参数

接受字符转类型
页面:

1
2
3
4
<form action="${pageContext.request.contextPath }/ test/recievestr.do" method="post">
id:<input type="text" name="name" id="name">
<input type="submit" value="提交">
</form>

接收参数方法:

1
2
3
4
@RequestMapping("recievestr")
public String recievestr(String name){
System.out.println(name);
return "login";}

接受数组:
分析:批量:checkbox复选框。Value必须有值
页面:

1
2
3
4
5
6
<form action="${pageContext.request.contextPath }/test/recievearray.do" method="post">
ID:<input type="checkbox" name="ids" value="1" id="ids">
ID:<input type="checkbox" name="ids" value="2" id="ids">
ID:<input type="checkbox" name="ids" value="3" id="ids">
<input type="submit" value="提交">
</form>

接受参数的方法

1
2
3
4
5
@RequestMapping("recievearray")
public String recievearray(Integer []ids){
System.out.println(ids);
return "login";
}

接受pojo类
页面:

1
2
3
4
5
<form action="${pageContext.request.contextPath }/test/recieveUser.do" method="post">
姓名:<input type="text" name="username" id="username">
生日:<input type="text" name="birthday" id="birthday">
<input type="submit" value="提交">
</form>

Pojo类

1
2
3
4
5
6
public class User {
private String username;
private Date birthday;
此处省略set。Get方法}

接受参数的方法:

1
2
3
4
5
@RequestMapping("recieveUser")
public String recievearray(User user){
System.out.println(user.getUsername());
return "login";
}

接受包装类型参数
包装pojo类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Usercus {
private User user;
此处省略set。Get方法。}
<form action="${pageContext.request.contextPath }/test/usercus.do" method="post">
姓名:<input type="text" name="user.username" id="username">
生日:<input type="text" name="user.birthday" id="birthday">
<input type="submit" value="提交">
</form>
接受参数的方法:
@RequestMapping("usercus")
public String usercus(Usercus user){
System.out.println(user.getUser().getUsername());
return "login";
}

接受集合类型参数
接受list集合
首先在包装类中加入如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private List<User> userlist;
此处省略set,get方法
然后是页面
<form action="${pageContext.request.contextPath }/test/recievelist.do" method="post">
姓名:<input type="text" name="userList[0].username" id="username">
地址:<input type="text" name="userList[0].address" id="address">
姓名:<input type="text" name="userList[1].username" id="username">
地址:<input type="text" name="userList[1].address" id="address">
<input type="submit" value="提交">
</form>
接受参数方法:
@RequestMapping("recievelist")
public String recievelist(Usercus userlist)
{
System.out.println(userlist.getUserlist().get(0).getUsername());
return "login";
}

热评文章