1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-
class
>org.springframework.web.servlet.DispatcherServlet</servlet-
class
>
<init-param>
<description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
|
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
|
<?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:p=
"http://www.springframework.org/schema/p"
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/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
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">
<!--
使Spring支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package=
"com.minx.crm.web.controller"
/>
<bean id=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix=
"/WEB-INF/jsp/"
p:suffix=
".jsp"
/>
</beans>
|
1
2
3
4
5
6
7
8
9
10
11
|
package com.minx.crm.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public
class
IndexController {
@RequestMapping(
"/index"
)
public
String index() {
return
"index"
;
}
}
|
1
2
3
4
5
6
7
8
9
|
@Controller
public
class
IndexController {
@RequestMapping(
"/index/{username}"
)
public
String index(@PathVariable(
"username"
) String username) {
System.out.
print
(username);
return
"index"
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Controller
public
class
LoginController {
@RequestMapping(value =
"/login"
, method = RequestMethod.GET)
public
String login() {
return
"login"
;
}
@RequestMapping(value =
"/login"
, method = RequestMethod.POST)
public
String login2(HttpServletRequest request) {
String username = request.getParameter(
"username"
).trim();
System.out.println(username);
return
"login2"
;
}
}
|
1
|
return
"redirect:/login2"
|
1
2
3
4
5
6
|
@RequestMapping(value =
"login"
, method = RequestMethod.POST)
public
String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter(
"username"
);
System.out.println(username);
return
null;
}
|
1
2
3
4
5
6
|
@RequestMapping(value =
"login"
, method = RequestMethod.POST)
public
String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam(
"username"
) String username) {
String username = request.getParameter(
"username"
);
System.out.println(username);
return
null;
}
|
1
2
3
4
5
|
@RequestMapping(value =
"login"
, method = RequestMethod.POST)
public
String testParam(PrintWriter out, @RequestParam(
"username"
) String username) {
out.println(username);
return
null;
}
|
1
2
3
4
5
6
7
|
public
class
User{
private
long id;
private
String username;
private
String password;
…此处省略getter,setter...
}
|
1
2
3
4
5
|
@RequestMapping(value =
"login"
, method = RequestMethod.POST)
public
String testParam(PrintWriter out, User user) {
out.println(user.getUsername());
return
null;
}
|
1
2
3
4
5
|
@RequestMapping(value =
"login"
, method = RequestMethod.POST)
public
String testParam(User user, Map model) {
model.put(
"user"
,user);
return
"view"
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
MyInteceptor
implements
HandlerInterceptor {
public
boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
throws Exception {
return
false;
}
public
void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
throws Exception {
}
public
void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
throws Exception {
}
}
|
1
2
3
4
5
6
|
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path=
"/index.htm"
/>
<bean
class
=
"com.minx.crm.web.interceptor.MyInterceptor"
/>
</mvc:interceptor>
</mvc:interceptors>
|
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="message"> </bean>
1
2
3
4
|
<bean id=
"messageSource"
class
=
"org.springframework.context.support.ResourceBundleMessageSource"
p:
basename
=
"message"
>
</bean>
|