一、基本环境:myeclipse8.5、tomcat 6.0.4、spring 3.1.1
二、搭建步骤
1、在web.xml里配置spring应用上下文和spring mvc的核心servlet,代码如下:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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/spring-servlet.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>
</web-app>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
lazy-init="false">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxActive">
<value>20</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
<property name="validationQuery">
<value>SELECT 'x' FROM DUAL</value>
</property>
<property name="connectionProperties">
<value>useUnicode=true;characterEncoding=utf-8</value>
</property>
</bean>
<!-- 配置spring-jdbcTemplate模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类
该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事物控制 -->
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="dataSourceTransactionManager" />
<property name="transactionAttributes">
<props>
<prop key="terParamsFileLeadingIn">PROPAGATION_REQUIRED</prop>
<prop key="batchSave">PROPAGATION_REQUIRED</prop>
<prop key="*">readOnly</prop>
</props>
</property>
</bean>
<!-- spring注解模式配置 -->
<context:annotation-config/>
<context:component-scan base-package="cn.eshore"/>
</beans>
spring配置文件中配置了数据源、spring jdbc、事务控制等等,另外采用了spring的注解配置,所以增加了最后两行。
3、配置spring mvc配置文件,文件默认位于web-info目录下,名称为web.xml中指定的servlet的名字springmvc-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: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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 启用spring mvc 注解 <context:annotation-config />-->
<mvc:annotation-driven />
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="cn.eshore.web"/>
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">error/error</prop>
<prop key="java.lang.Throwable">error/error</prop>
</props>
</property>
<property name="statusCodes">
<props>
<prop key="error/error">500</prop>
<prop key="error/error">404</prop>
</props>
</property>
<!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->
<property name="warnLogCategory" value="WARN"></property>
<!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->
<property name="defaultErrorView" value="error/error"></property>
<!-- 默认HTTP状态码 -->
<property name="defaultStatusCode" value="500"></property>
</bean>
<!-- 定义跳转的文件的前后缀 ViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
如果需要在controller里返回json数据,则需要启用
<mvc:annotation-driven />
一般程序启用<context:annotation-config />即可,此段代码已在spring配置文件中配置,则此处无需配置。
4、编写测试的controlller
package cn.eshore.web;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.eshore.beans.App;
import cn.eshore.services.TestService;
@RequestMapping("/hello")
@Controller
public class TestController{
@Autowired
public JdbcTemplate jdbcTemplate;
@Resource
private TestService testService;
@RequestMapping("/tests") // 请求url地址映射,类似Struts的action-mapping
public ModelAndView tests(HttpServletRequest request){
ModelAndView mv =new ModelAndView();
// mv.addObject("message", testService.getData());
mv.addObject("message", "xxxxxxxxxxxxxxxx");
mv.setViewName("hello");
//int i=2/0;
return mv;
}
@RequestMapping("/testJson")
public @ResponseBody App getJson(){
App app=new App();
app.setId("111");
app.setLabelName("xiaoming");
return app;
}
}
App类结构:
package cn.eshore.beans;
public class App {
public String id; //用户编号
public String labelName; //手机号
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabelName() {
return labelName;
}
public void setLabelName(String labelName) {
this.labelName = labelName;
}
}
5、访问一般测试页面:
结果如下图:
测试json: