这里介绍一下快速搭建SpringMVC,并使用DBHelper操作数据库,Hibernate我是基本不用了,ibatis又比较麻烦(看项目需求吧),没有DBHelper来的自由灵活。
首先新建一个java web项目,起好名字,建好包结构后。然后加入Spring的支持,myeclispe下邮件增加Spring支持。只需选上默认的Spring 3.0 core libraries 。生成的applicationContext.xml,作为模版文件使用。首先在WEB-INF目录下,新建一个xml文件(可以直接复制applicationContext.xml),我一般叫:项目名-servlet.xml,它的功能主要是使用注解加载所有的类控制器类,以及设置对模型视图名称的解析,和异常处理等等。具体的看注释就可以了。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 自动扫描com.bdqns.web 包下的@Controller标注的类控制器类 --> <context:component-scan base-package="com.crt.web"/> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages" /> <!--WEB异常解析处理--> <bean id="exceptionResolver" class="com.crt.crtHandlerExceptionResolver"> <property name="defaultErrorView"> <value>fail</value> </property> <property name="exceptionMappings"> <props> <prop key="java.lang.RuntimeException">fail</prop> </props> </property> </bean> </beans>
然后,在src目录下新建一个项目名.xml文件(可以直接复制applicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 数据访问层配置 --> <import resource="classpath:crt-dao.xml" /> <!--服务层配置 --> <import resource="classpath:crt-service.xml" /> </beans>
在src目录下新建一个项目名-dao.xml文件(可以直接复制applicationContext.xml),它主要负责扫描com.crt.dao包下所有标注@Repository的DAO组件。具体看代码。
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描com.crt.dao包下所有标注@Repository的DAO组件 --> <context:component-scan base-package="com.crt.dao"/> </beans>
在src目录下新建一个项目名-service.xml文件(可以直接复制applicationContext.xml),它主要负责扫描com.crt.service包下所有标注@Service的服务组件,如果使用缓存也可以在此设置。具体看代码。
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 扫描com.crt.service包下所有标注@Service的服务组件 --> <context:component-scan base-package="com.crt.service"/> <!-- 基于EHCache的系统缓存管理器--> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/> </beans>
在src目录下新建一个ehcache.xml文件(可以直接复制applicationContext.xml),进行一些缓存的设置。具体看代码。
<ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <diskStore path="java.io.tmpdir" /> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required: maxElementsInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. The following attributes are optional: timeToIdleSeconds - Sets the time to idle for an element before it expires. i.e. The maximum amount of time between accesses before an element expires Is only used if the element is not eternal. Optional attribute. A value of 0 means that an Element can idle for infinity. The default value is 0. timeToLiveSeconds - Sets the time to live for an element before it expires. i.e. The maximum time between creation time and when an element expires. Is only used if the element is not eternal. Optional attribute. A value of 0 means that and Element can live for infinity. The default value is 0. diskPersistent - Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value is 120 seconds. --> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="0" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <cache name="fixedRegion" maxElementsInMemory="100" eternal="true" overflowToDisk="false"/> <cache name="freqChangeRegion" maxElementsInMemory="5000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="1800"/> </ehcache>
在每个dao类开头上要加上@Repository
@Repository public class xxxxxxDAO {......}
在每个serivce类开头上加上@Service
@Service public class xxxxxxService {......}
在controller类开头加上
@Controller @RequestMapping("/xxx") public class AlarmInfoController {........}
其中,@RequestMapping("/xxx")标识该controller。
在controller类中的每个方法上面加上@RequestMapping("/doxxxmmm")注意其中的xxx要和上面的xxx相同
@RequestMapping("/doxxxmmm") //报警监控 public void doxxxmmm(HttpServletRequest request,HttpServletResponse response) throws ParseException {......}
举个例子:
url:"<%=basePath%>alarmInfo/doalarmajax.action";这样去找到哪个controller中对应的方法。
最后需要去配置web.xml。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <description>项目名</description> <display-name>项目名</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:项目名.xml</param-value> </context-param> <listener> <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <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> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <servlet> <servlet-name>crt</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>项目名</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc 需要用到的jar包在附件里了。