导包
Spring需要用的包
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC_SVN</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置一个全局初始化参数,用于指定spring的配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 配置SpringMVC的核心控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC的配置文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.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>
<!-- 处理POST提交的中文乱码 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Spring.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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置Ehcache的bean 静态工厂方式 -->
<bean class="net.sf.ehcache.CacheManager" factory-method="create"></bean>
<!-- 扫描包 -->
<context:component-scan base-package="cn.itsource.dao.impl,cn.itsource.service.impl" />
<!-- 数据库properties文件 -->
<context:property-placeholder location="classpath:dbcp.properties"/>
<!-- 配置数据源 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SpringJDBC的JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds" />
</bean>
</beans>
SpringMVC配置
<?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/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">
<!-- Spring的配置拦截器,可以配置多个 ,不好用,不推荐使用 -->
<mvc:interceptors>
<!-- 配置一个拦截器 -->
<mvc:interceptor>
<!-- 当前拦截器拦截的请求路径 -->
<mvc:mapping path="/**"/>
<!-- 设置直接放行的请求路径 -->
<mvc:exclude-mapping path="/user/login.do"/>
<mvc:exclude-mapping path="/user/reg.do"/>
<mvc:exclude-mapping path="/user/logout.do"/>
<mvc:exclude-mapping path="/reg.jsp"/>
<mvc:exclude-mapping path="/login.jsp"/>
<!-- <mvc:exclude-mapping path="/show/*"/>不拦截前台用户的所有请求 -->
<!-- 静态资源放行 -->
<mvc:exclude-mapping path="/css/*.css"/>
<mvc:exclude-mapping path="/fonts/*"/>
<mvc:exclude-mapping path="/js/*.js"/>
<mvc:exclude-mapping path="/imgs/*"/>
<mvc:exclude-mapping path="/carousel/*"/>
<mvc:exclude-mapping path="*.html"/>
<!-- 自定义拦截器类的完全限定名 -->
<bean class="cn.itsource.interceptor.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<!-- 开启支持注解【默认就已经是开启的】 -->
<mvc:annotation-driven />
<!-- 静态资源放行 DispatcherServlet的url-pattern配置为 / 的时候使用 -->
<mvc:default-servlet-handler/>
<!--
扫描包【自动递归扫描当前包和它内部的所有子包】
扫描四种注解@Controller、@Service、@Repository、@Component
创建对象~~保存到spring容器中~~以类名称为键,以创建的对象为值
-->
<context:component-scan base-package="cn.itsource.controller" />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 指定视图的前缀 -->
<property name="prefix" value="/WEB-INF/admin/"/>
<!-- 指定视图的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置文件上传相关的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>104857600</value><!-- 最大100M -->
</property>
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>
Spring过滤器的配套代码
package cn.itsource.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("处理请求之后");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("正在处理请求");
}
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2) throws Exception {
System.out.println("Controller中的接收请求方法执行之前执行");
//验证是否已经登录
//让某些请求直接放行
HttpSession session = req.getSession();
Object obj = session.getAttribute("user");
if(obj == null) {
session.setAttribute("msg", "请先登录!");
resp.sendRedirect(req.getContextPath() + "/login.jsp");
return false;
}
return true;
}
}
dbcp.properties数据库连接池配置
#连接设置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cms
jdbc.username=root
jdbc.password=root
#<!-- 初始化连接数量 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最大空闲连接 -->
maxIdle=20
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=utf8
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_COMMITTED
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/75238.html