SpringMVC (+ acquaintance Procedure)

1. Import Package
Configuration 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">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
3. Create the core profile springmvc
- "servlet.xml" the name of a central controller (servlet's name) +: file naming convention
under WEB-INF: The default location

Head of information springmvc

<?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.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4.写controller

@controller: identifies the current class is a particular implementation of the control layer
@requestMapping: in the above method is used to specify a path to a method of, when it is based on the time corresponding to the namespace requestmapping combined method needs to access .

@Controller
@RequestMapping("/test")
public class TestController {

@RequestMapping("/hello.do")
public String hello() {

return "index";
}

@RequestMapping("/reparam.do")
public String reparam(HttpServletRequest req) {
String name=req.getParameter("name");
System.out.println(name);
return "index";
}

/ *
* Underlayer go is the HttpServletRequest
* /
@RequestMapping ( "/ re2.do")
public String RE2 (String name) {

System.out.println (name);
return "index";

}

@RequestMapping("/re3.do")
public String re3(String name,Integer id,Date bir) {

System.out.println(name+" "+id+" "+bir);
return "index";

}

@InitBinder
public void initBind(ServletRequestDataBinder binder) {

binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
}


@RequestMapping("/toform.do")
public String toform() {

return "form";
}

@RequestMapping("/form.do")
public String form(String[] favor) {

for(String fa :favor)
System.out.println(fa);

return "index";
}

@RequestMapping("/toperson.do")
public String toperson(Person person) {

System.out.println(person);
return "success";
}

//参数map是往页面写的
@RequestMapping("/getP.do")
public String getP(Map<String, Object> map) {
Person p1=new Person(2, "lisi", "bj", 2, new Date());
map.put("person", p1);
return "returnPage";
}

//最常用的
@RequestMapping("/getM.do")
public String getM(Model model) {
Person p1=new Person(2, "lisi", "bj", 2, new Date());
model.addAttribute("person", p1);
return "returnPage";
}
}
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
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
52 is
53 is
54 is
55
56 is
57 is
58
59
60
61 is
62 is
63 is
64
65
66
67
68
69
70
71 is
72
73 is
74
75
76
77
78
79
80
81
82
1 can own in the method to arbitrarily define the parameters of the method, if the parameter name matches the name parameter passed method will automatically receive, convert the data type and we do not define. If the parameter list defines a class springmvc custom will give us the parameters to match up the phone and assembled into objects.
2. requestMapping type of method which must be consistent with the type of reception form

//-------------------------------------------------------------------------------------------------------

The view resolver (written 3 <beans> </ beans> inside)

<the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<Property name = "prefix" value = "/ the WEB-INF / JSP /"> </ Property>
<Property name = "suffix" value = ".jsp"> </ Property>
</ the bean>
. 1
2
. 3
. 4
6. the annotation specifies springmvc driving profile arranged scanner
(scanner would not have opened driven)

<context:component-scan base-package="com.rl.controller"></context:component-scan>

Guess you like

Origin www.cnblogs.com/hyhy904/p/10962019.html