鉴于现在还是有很多项目用xml形式来整合,所以又来整了一遍
上篇基于java config的链接 : http://www.jianshu.com/p/8fcefc4d724b
需要的环境与上一篇一致,其他相同的配置在这里也不再重写一遍了
-
new module,选择maven , Create From archetype,然后一路next,填写自己的包名
-
点击finish之后,idea会自动使用maven进行build,build后你会发现在src/main下没有java夹子
-
新建一个java夹子,然后打开Project-Settings,使java夹子Mark as Sources,最后Apply,Ok.
- 以上就建立了一个空的module,最后的工程结构如下图所示
这里大致的结构与上一篇相似,明显不同的在于多了一个webapp,这里放了静态资源,views,spring配置以及web.xml - web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Spring demo by Sam</display-name>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>my-spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-spring</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>
- my-spring-servlet.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"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!--指向webapp/resources-->
<mvc:resources mapping="/resources/**"
location="/resources/"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<context:annotation-config/>
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.sam"/>
<!--比如DBException可以配置成跳转登陆-->
<bean id="handlerExceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.sam.exception.DBException">/login</prop>
</props>
</property>
</bean>
<mvc:interceptors>
<!--配置拦截器-->
<mvc:interceptor>
<mvc:mapping path="/"/>
<bean class="com.sam.interceptor.MySpringInterceptor">
<property name="excludedUrls">
<list>
<value>/login</value>
<value>/register</value>
<value>/home</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
<!--配置thymeleaf-->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="staticVariables">
<map>
<entry key="e" value-ref="thymeleafUtils"></entry>
</map>
</property>
</bean>
<!--此处有坑,暂未填完,staticVariables没有生效-->
<bean id="thymeleafUtils" class="com.sam.common.SpringUtils"/>
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
</bean>
<!--此处填上你的实例名,用户名以及密码-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/YOUR_INSTANCE_NAME"/>
<property name="username" value="YOUR_USER"/>
<property name="password" value="YOUR_PASSWORD"/>
</bean>
<!--自动扫描mapper接口,这里需要加上mybatis的namespace,不然无法使用-->
<mybatis:scan base-package="com.sam.dao"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/sam/dao/*.xml"/>
</bean>
</beans>
- MySpringInterceptor
package com.sam.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Created by sam on 16/5/31.
*/
public class MySpringInterceptor implements HandlerInterceptor {
//不需要拦截的路径,如登陆注册首页
private List<String> excludedUrls;
public void setExcludedUrls(List<String> excludedUrls) {
this.excludedUrls = excludedUrls;
}
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String requestUri = httpServletRequest.getRequestURI();
for (String url : excludedUrls) {
if (requestUri.endsWith(url)) {
//在xml中初始化不需要拦截的url
return true;
}
}
// do sth;
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
- Finally ,访问 http://localhost:8080/YOUR_MODULE_NAME/hello/world 来试一下吧
- ps : thymeleaf的staticVariables没有生效,如果有大牛看到,请在评论里给我答疑 _