maven构建springmvcWord格式文档下载.docx
《maven构建springmvcWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《maven构建springmvcWord格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。
?
xmlversion="
1.0"
encoding="
UTF-8"
>
web-appxmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="
xmlns:
web="
xsi:
schemaLocation="
id="
study"
version="
2.5"
<
display-name>
ArchetypeCreatedWebApplication<
/display-name>
description>
sprintMVC环境搭建<
/description>
!
--加载Spring配置文件-->
context-param>
<
param-name>
contextConfigLocation<
/param-name>
param-value>
classpath:
/configs/spring-*.xml<
/param-value>
/context-param>
--Spring监听-->
listener>
listener-class>
org.springframework.web.context.ContextLoaderListener<
/listener-class>
/listener>
--SpringMVC配置-->
servlet>
servlet-name>
Dispatcher<
/servlet-name>
servlet-class>
org.springframework.web.servlet.DispatcherServlet<
/servlet-class>
--自定义springmvc的配置文件名称和路径-->
init-param>
<
configs/spring-servlet.xml<
/init-param>
load-on-startup>
1<
/load-on-startup>
/servlet>
--springmvc请求后缀-->
servlet-mapping>
url-pattern>
*.do<
/url-pattern>
/servlet-mapping>
/web-app>
Web.xml文件设置完后,需要创建一个配置文件,用于配置
在src/main/resources/configs下创建spring-servlet.xml文件。
文件内容下面有两种
上面头部分都是一样的,只有下面bean中的信息不一样
第一种是,映射到jsp到,从controller中返回到jsp中;
第二种是,controller返回json数据,返回json数据,需要添加下面的配置,是json对象转换的类包
beansxmlns="
//www.springframework.org/schema/beans"
jee="
//www.springframework.org/schema/jee"
context="
//www.springframework.org/schema/context"
p="
//www.springframework.org/schema/p"
mvc="
//www.springframework.org/schema/mvc"
util="
//www.springframework.org/schema/util"
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-4.1.xsd
http:
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-4.0.xsd
//www.springframework.org/schema/jee
//www.springframework.org/schema/jee/spring-jee-4.1.xsd
//www.springframework.org/schema/mvc
//www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
//www.springframework.org/schema/util
//www.springframework.org/schema/util/spring-util-4.1.xsd"
context:
annotation-config/>
component-scanbase-package="
demo.test"
/>
beanclass="
org.springframework.web.servlet.view.InternalResourceViewResolver"
propertyname="
prefix"
value="
/WEB-INF/views/"
suffix"
value="
.jsp"
/bean>
/beans>
bean
class="
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
messageConverters"
list>
<
class="
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
supportedMediaTypes"
value="
application/json;
charset=utf-8;
"
/list>
/property>
Controller测试
首先创建一个包。
其次创建包下的类
一个是controller类,一个用于转换json的测试类
@Controller
publicclassTestController{
@RequestMapping("
/hello"
)
publicModelAndViewhello(){
ModelAndViewmv=newModelAndView();
mv.addObject("
spring"
"
springmvc"
);
mv.setViewName("
hello"
returnmv;
}
/h"
@ResponseBody
publicResultgettest(HttpServletResponseresponse){
response.setHeader("
Access-Control-Allow-Origin"
*"
Resulta=newResult();
a.setA("
tttt"
returna;
}