参数封装

 实体类:

 1 package com.jove.domain;
 2 
 3 
 4 import java.util.Date;
 5 
 6 /**
 7  * Created by 罗俊杰 on 2018/1/12.<br/>
 8  * description:
 9  */
10 public class User {
11     private Integer id;
12     private String username;
13     private Date birthday;
14     private String sex;
15     private String address;
16 
17     public Integer getId() {
18         return id;
19     }
20 
21     public void setId(Integer id) {
22         this.id = id;
23     }
24 
25     public String getUsername() {
26         return username;
27     }
28 
29     public void setUsername(String username) {
30         this.username = username;
31     }
32 
33     public Date getBirthday() {
34         return birthday;
35     }
36 
37     public void setBirthday(Date birthday) {
38         this.birthday = birthday;
39     }
40 
41     public String getSex() {
42         return sex;
43     }
44 
45     public void setSex(String sex) {
46         this.sex = sex;
47     }
48 
49     public String getAddress() {
50         return address;
51     }
52 
53     public void setAddress(String address) {
54         this.address = address;
55     }
56 
57     @Override
58     public String toString() {
59         return "User{" +
60                 "id=" + id +
61                 ", username='" + username + '\'' +
62                 ", birthday=" + birthday +
63                 ", sex='" + sex + '\'' +
64                 ", address='" + address + '\'' +
65                 '}';
66     }
67 }
View Code
 1 package com.jove.domain;
 2 
 3 import java.util.Arrays;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 /**
 9  * Created by 罗俊杰 on 2018/1/14.<br/>
10  * description:
11  */
12 public class UserExt {
13     private User user;
14     private String password;
15     private String hobby;
16     private List<User> userList;
17     private Integer[] ids;
18     private Map<String, Object> maps = new HashMap<>();
19 
20     public Map<String, Object> getMaps() {
21         return maps;
22     }
23 
24     public void setMaps(Map<String, Object> maps) {
25         this.maps = maps;
26     }
27 
28     public Integer[] getIds() {
29         return ids;
30     }
31 
32     public void setIds(Integer[] ids) {
33         this.ids = ids;
34     }
35 
36     public List<User> getUserList() {
37         return userList;
38     }
39 
40     public void setUserList(List<User> userList) {
41         this.userList = userList;
42     }
43 
44     public String getPassword() {
45         return password;
46     }
47 
48     public void setPassword(String password) {
49         this.password = password;
50     }
51 
52     public String getHobby() {
53         return hobby;
54     }
55 
56     public void setHobby(String hobby) {
57         this.hobby = hobby;
58     }
59 
60     public User getUser() {
61         return user;
62     }
63 
64     public void setUser(User user) {
65         this.user = user;
66     }
67 
68     @Override
69     public String toString() {
70         return "UserExt{" +
71                 "user=" + user +
72                 ", password='" + password + '\'' +
73                 ", hobby='" + hobby + '\'' +
74                 ", userList=" + userList +
75                 ", ids=" + Arrays.toString(ids) +
76                 ", maps=" + maps +
77                 '}';
78     }
79 }
View Code

控制层:

 1 package com.jove.controller;
 2 
 3 import com.jove.domain.User;
 4 import com.jove.domain.UserExt;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 
 8 import java.util.Arrays;
 9 import java.util.List;
10 
11 @Controller
12 @RequestMapping("user")
13 public class UserController {
14     @RequestMapping("hello")
15     public String hello() {
16         return "hello";
17     }
18 
19     @RequestMapping("toAdd")
20     public String add() {
21         return "add";
22     }
23 
24     @RequestMapping("recieveInt")
25     public String receiveInt(Integer id) {
26         System.out.println(id);
27         return "success";
28     }
29 
30     @RequestMapping("recieveStr")
31     public String receiveStr(String username) {
32         System.out.println(username);
33         return "success";
34     }
35 
36     @RequestMapping("recieveUser")
37     public String recieveUser(User user) {
38         System.out.println(user);
39         return "success";
40     }
41 
42     @RequestMapping("recieveUserExt")
43     public String recieveUserExt(UserExt userExt) {
44         System.out.println(userExt);
45         return "success";
46     }
47 
48     @RequestMapping("recieveArray")
49     public String recieveUserExt(Integer[] ids) {
50         System.out.println(Arrays.toString(ids));
51         return "success";
52     }
53 
54     @RequestMapping("recieveUserList")
55     public String recieveUserList(UserExt userExt) {
56         System.out.println(userExt);
57         return "success";
58     }
59 
60     @RequestMapping("recieveMap")
61     public String recieveMap(UserExt userExt) {
62         System.out.println(userExt);
63         return "success";
64     }
65 
66 }
View Code

add.jsp

 1 <%@ page language="java" pageEncoding="UTF-8" %>
 2 <%
 3     String path = request.getContextPath();
 4     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
 5             + path + "/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10 <head>
11     <base href="<%=basePath%>">
12 
13     <title>My JSP 'hello.jsp' starting page</title>
14 
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21         <link rel="stylesheet" type="text/css" href="styles.css">
22         -->
23 
24 </head>
25 
26 <body>
27 <form action="${pageContext.request.contextPath }/user/recieveInt.do" method="post">
28     ID:<input type="text" name="id" id="id"> <input type="submit" value="提交">
29 </form>
30 <hr style="background: aqua;height: 5px;"/>
31 
32 <form action="${pageContext.request.contextPath }/user/recieveStr.do" method="post">
33     username:<input type="text" name="username" id="username"> <input type="submit" value="提交">
34 </form>
35 <hr style="background: aqua;height: 5px;"/>
36 
37 <form action="${pageContext.request.contextPath }/user/recieveUser.do" method="post">
38     birthday:<input type="text" name="birthday" id="birthday">
39     username:<input type="text" name="username" id="username">
40     sex:<input type="text" name="sex" id="sex">
41     address:<input type="text" name="address" id="address">
42     <input type="submit" value="提交">
43 </form>
44 <hr style="background: aqua;height: 5px;"/>
45 
46 <form action="${pageContext.request.contextPath }/user/recieveUserExt.do" method="post">
47     birthday:<input type="text" name="user.birthday" id="birthday">
48     username:<input type="text" name="user.username" id="username">
49     password:<input type="text" name="password" id="password">
50     hobby:<input type="text" name="hobby" id="hobby">
51     <input type="submit" value="提交">
52 </form>
53 <hr style="background: aqua;height: 5px;"/>
54 
55 <form action="${pageContext.request.contextPath }/user/recieveArray.do" method="post">
56     checkbox:<input type="checkbox" name="ids" value="1" id="ids">
57     <input type="checkbox" name="ids" value="2" id="ids">
58     <input type="checkbox" name="ids" value="3">
59     <input type="checkbox" name="ids" value="4">
60     <input type="submit" value="提交">
61 </form>
62 <hr style="background: aqua;height: 5px;"/>
63 
64 <form action="${pageContext.request.contextPath }/user/recieveMap.do" method="post">
65 
66     <hr style="background: aqua;height: 5px;"/>
67     username:<input type="text" name="maps['username']" id="username" >
68     address: <input type="text" name="maps['address']" id="address">
69     <input type="submit" value="提交">
70 </form>
71 <hr style="background: aqua;height: 5px;"/>
72 </body>
73 </html>
View Code

web.xml

 1 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2          xmlns="http://java.sun.com/xml/ns/javaee"
 3          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4          id="WebApp_ID" version="2.5">
 5     <display-name>Archetype Created Web Application</display-name>
 6     <filter>
 7         <filter-name>character</filter-name>
 8         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 9         <init-param>
10             <param-name>encoding</param-name>
11             <param-value>utf-8</param-value>
12         </init-param>
13     </filter>
14     <filter-mapping>
15         <filter-name>character</filter-name>
16         <url-pattern>/*</url-pattern>
17     </filter-mapping>
18     <servlet>
19         <servlet-name>springmvc</servlet-name>
20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
21         <init-param>
22             <param-name>contextConfigLocation</param-name>
23             <param-value>classpath:config/springmvc.xml</param-value>
24         </init-param>
25     </servlet>
26     <servlet-mapping>
27         <servlet-name>springmvc</servlet-name>
28         <url-pattern>*.do</url-pattern>
29     </servlet-mapping>
30 </web-app>
View Code

springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 8         http://www.springframework.org/schema/mvc
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context-3.1.xsd
12         http://www.springframework.org/schema/aop
13         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
14         http://www.springframework.org/schema/tx
15         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
16     <!-- 默认注册了注解映射器和注解适配器等bean -->
17     <!-- 扫描包下的注解 -->
18     <context:component-scan base-package="com.jove"></context:component-scan>
19     <!-- 处理器映射器,处理器适配器 -->
20     <mvc:annotation-driven/>
21     <!-- 视图解析器 -->
22     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23         <property name="prefix" value="/WEB-INF/jsps/"></property>
24         <property name="suffix" value=".jsp"></property>
25     </bean>
26 </beans>
View Code

 

posted on 2018-01-14 21:54  jovelove  阅读(113)  评论(0)    收藏  举报