参考了下文档,没有什么好说的
WEB.XML
<display-name>homminns</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>homminns.root</param-value> </context-param> <!-- 防止内存溢出 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!--Log4j配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/conf/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- DispatcherServlet配置 --> <servlet> <servlet-name>SpringMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/SpringMvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMvc</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/applicationContext*.xml</param-value> </context-param> <!--Spring初始化容器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符乱码配置 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>cn.base.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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 装饰器 --> <filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>*.shtml</url-pattern> </filter-mapping> <session-config> <session-timeout>15</session-timeout> </session-config> <!-- 错误页 <error-page> <error-code>404</error-code> <location>/index.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/index.jsp</location> </error-page> --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 配置数据源 换成 druid --> <context:property-placeholder location="/WEB-INF/conf/jdbc.properties" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="acquireIncrement" value="${c3p0.acquireIncrement}" /> <property name="initialPoolSize" value="${c3p0.initialPoolSize}" /> <property name="minPoolSize" value="${c3p0.minPoolSize}" /> <property name="maxPoolSize" value="${c3p0.maxPoolSize}" /> <property name="maxIdleTime" value="${c3p0.maxIdleTime}" /> <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" /> <property name="maxStatements" value="${c3p0.maxStatements}" /> <property name="numHelperThreads" value="${c3p0.numHelperThreads}" /> </bean> <!-- 配置JDBC--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <!--Hibernate配置 --> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="lobHandler" ref="lobHandler" /> <property name="mappingDirectoryLocations" value="classpath:/cn/base/web/domain/" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> <prop key="hibernate.format_sql"> ${hibernate.format_sql} </prop> <prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size} </prop> <prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size} </prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <constructor-arg ref="sessionFactory" /> </bean> <!--事务配置 --> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--事务通知 --> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="query*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" /> </tx:attributes> </tx:advice> <!--AOP配置 --> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* *..*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" /> </aop:config> </beans>
SpringMVC.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:p="http://www.springframework.org/schema/p" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 设置freeMarker的配置文件路径 --> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="/WEB-INF/conf/freemarker.properties" /> </bean> <!-- 配置freeMarker --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/template/" /> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> <property name="freemarkerSettings"> <props> <prop key="classic_compatible">true</prop> <prop key="tag_syntax">auto_detect</prop> <prop key="template_update_delay">10</prop> <prop key="defaultEncoding">UTF-8</prop> <prop key="url_escaping_charset">UTF-8</prop> <prop key="locale">zh_CN</prop> <prop key="boolean_format">true,false</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="number_format">0.######</prop> <prop key="whitespace_stripping">true</prop> </props> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" /> <!-- 配置freeMarker视图解析器 --> <bean id="freemakerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <!-- <property name="prefix" value="/WEB-INF/template/" /> --> <property name="suffix" value=".html" /> <property name="contentType" value="text/html; charset=UTF-8" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="requestContextAttribute" value="request" /> <property name="order" value="1" /> </bean> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/page/" p:suffix=".jsp" p:order="2" /> <!-- 扫描注解类 --> <context:component-scan base-package="cn.base.web.controller" /> <context:component-scan base-package="cn.base.web.dao.impl" /> <context:component-scan base-package="cn.base.web.service.impl" /> </beans>
View
@RequestMapping(value = "/loadHello.shtml") public String loadHello(HttpServletRequest request, HttpServletResponse response) { List<User> list = new ArrayList<User>(); for (int i = 0; i < 20; i++) { User user = new User(); user.setUsername("Tom" + i); user.setEmail("365687@189.com"); user.setNickname("I'am irving"); user.setId(i); user.setPassword(i + "Irving"); user.setAdddate(new Date()); list.add(user); } request.setAttribute("list", list); return "hello"; } @RequestMapping(value = "/loadHellojsp.shtml") public String loadHellojsp(HttpServletRequest request, HttpServletResponse response) { List<User> list = new ArrayList<User>(); for (int i = 0; i < 20; i++) { User user = new User(); user.setUsername("Tom" + i); user.setEmail("365687@xx.com"); user.setNickname("I'am irving"); user.setId(i); user.setPassword(i + "Irving"); user.setAdddate(new Date()); list.add(user); } request.setAttribute("list", list); return "hellojsp"; }
hello.html
<table> <tr><td>index</td><td>username</td><td>password</td><td>email</td><td>adddate</td></tr> <#list list as user> <tr> <td>${user.id}</td> <td>$${user.username}</td> <td>${user.password}</td> <td>${user.email}</td> <td>${user.adddate?date}</td> </tr> </#list> </table>
freemaker:http://localhost:8080/springmvc/loadHello.shtml
jsp:http://localhost:8080/springmvc/loadHellojsp.shtml
参考:
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html