SpringMVC搭建
2016-01-12 18:15
609 查看
SpringMVC搭建
一、首先建立一个web项目,新建项目,我就不写了,可参考之前的idea 新建maven web项目,建好之后,在pom.xml中加入上一篇中的spring依赖二、相关准备配置
1、在项目中,建立大概的目录结构,就是我们所谓的controller包、service包、dao包、entity包
2、配置文件:spring-mvc.xml、application.xml、web.xml
spring-mvc.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.think.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"> <mvc:message-converters register-defaults="true"> <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> <!-- 使用fastjson转换 --> <!--<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">--> <!--<property name="features">--> <!--<list>--> <!--<value>WriteEnumUsingToString</value>--> <!--<value>PrettyFormat</value>--> <!--</list>--> <!--</property>--> <!--</bean>--> </mvc:message-converters> <!--<mvc:return-value-handlers>--> <!--<bean class="com.project.handler.UserHandlers"></bean>--> <!--</mvc:return-value-handlers>--> </mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="contentNegotiationManager" ref="contentNegotiationManager"></property> <property name="order" value="0"></property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="1"></property> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="2"></property> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView"> <property name="extractValueFromSingleKeyModel" value="true"></property> </bean> </list> </property> </bean> <!-- REST中根据URL后缀自动判定Content-Type及相应的View --> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="ignoreAcceptHeader" value="false" /> <!--text/html--> <property name="defaultContentType" value="text/html" /> <property name="mediaTypes"> <value> json=application/json xml=application/xml </value> </property> </bean> <!--<mvc:interceptors>--> <!--<mvc:interceptor>--> <!--<mvc:mapping path="/user/**/" />--> <!--<bean class="com.project.interceptor.UserInterceptor">--> <!--<property name="allowUrls">--> <!--<list>--> <!--<!– 如果请求中包含以下路径,则不进行拦截 –>--> <!--<value>/writeSession</value>--> <!--<value>/login</value>--> <!--<value>/js</value>--> <!--<value>/css</value>--> <!--<value>/image</value>--> <!--<value>/images</value>--> <!--</list>--> <!--</property>--> <!--</bean>--> <!--</mvc:interceptor>--> <!--</mvc:interceptors>--> <mvc:default-servlet-handler /> </beans>
application.xml
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="false"> </beans>
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_2_5.xsd" version="2.5"> <display-name>Archetype Created Web Application</display-name> <!-- 设置spring上下文配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring监听类 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC核心控制 --> <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:spring-mvc.xml</param-value> </init-param> </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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
相关文章推荐
- struts2中s:iterator 标签的使用详解
- eclipse 安装反编译插件
- Java 8 学习–lambda表达式
- Java注解(1)-基础
- 40个Java多线程问题总结
- java.util.Collection体系源码解读<二>Collection接口源码
- Java-Map
- springmvc开发--IOC和AOP
- Java-Map
- Java-Map
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- Java [Leetcode 110]Balanced Binary Tree
- 详解Spring MVC 4常用的那些注解
- 查看java类是从哪个包加载
- 在spring MVC的controller中获取ServletConfig
- SSL介绍与Java实例
- Selenium+java环境搭建
- Android with Eclipse - Waiting for HOME ('android.process.acore') to be launched?
- java图片加水印文字
- java中遍历Map集合的集中方法总结