SpringMVC多视图技术很强大,举例来说,你访问同一个控制器的方法,可以根据请求方式不同,返回不同的格式的数据。
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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app version=
"2.5"
xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:
//java.sun.com/xml/ns/javaee
http:
//java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Lrtech_framework</display-name>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-
class
>org.springframework.web.servlet.DispatcherServlet</servlet-
class
>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/conf/springdemo-servlet.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>
true
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|
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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!--suppress XmlUnboundNsPrefix -->
<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"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
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
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解驱动 -->
<mvc:annotation-driven/>
<context:component-scan base-
package
=
"com.lavasoft.demo.web.controller"
/>
<mvc:resources mapping=
"/ui/**"
location=
"/ui/"
/>
<bean
class
=
"org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
>
<property name=
"order"
value=
"1"
/>
<property name=
"mediaTypes"
>
<map>
<entry key=
"json"
value=
"application/json"
/>
</map>
</property>
<property name=
"defaultViews"
>
<list>
<bean
class
=
"org.springframework.web.servlet.view.json.MappingJacksonJsonView"
/>
</list>
</property>
<property name=
"ignoreAcceptHeader"
value=
"true"
/>
</bean>
<bean
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name=
"prefix"
value=
"/demo/"
/>
<property name=
"suffix"
value=
".jsp"
/>
</bean>
</beans>
|
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
|
package
com.lavasoft.demo.web.controller.lsh.ch5;
import
org.springframework.stereotype.Controller;
import
org.springframework.ui.ModelMap;
import
org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by Administrator on 14-4-9.
*
* @author leizhimin 14-4-9 上午10:55
*/
@Controller
@RequestMapping(
"/demo/lsh/ch5"
)
public
class
MultViewController {
@RequestMapping(
"/show"
)
public
String
toShow(ModelMap model) {
User user =
new
User();
user.setUserName(
"testuname"
);
user.setAge(
"23"
);
model.put(
"user"
, user);
return
"/lsh/ch5/show"
;
}
@RequestMapping(
"/demo/lsh/ch5/test"
)
public
String
test(){
return
"/lsh/ch5/test"
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%--
Created by IntelliJ IDEA.
User: leizhimin
14
-
4
-
9
上午
10
:
57
--%>
<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<html>
<head>
<title></title>
</head>
<body>
<h1>SpringMVC多视图</h1>
${user.userName} <br />
${user.age}
</body>
</html>
|
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1392889,如需转载请自行联系原作者