spring+spring mvc+spring jdbc

时间:2025-03-02 08:59:14

一、基本环境:myeclipse8.5、tomcat 6.0.4、spring 3.1.1

二、搭建步骤

1、在里配置spring应用上下文和spring mvc的核心servlet,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http:///2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>
  
  <listener>
    <listener-class></listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:</param-value>
  </context-param>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class></servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/</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>

2、在src目录下配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
 xmlns:xsi="http:///2001/XMLSchema-instance"
 xmlns:aop="/schema/aop"
 xmlns:tx="/schema/tx"
 xmlns:context="/schema/context"
 xsi:schemaLocation="
       /schema/beans 
       /schema/beans/
       /schema/tx 
       /schema/tx/
       /schema/aop 
       /schema/aop/
       /schema/context
       /schema/context/">
       
	<bean 
		class=""
		lazy-init="false">
		<property name="locations">
			<list>
				<value>classpath:</value>
			</list>
		</property>
	</bean>

	<bean  class="" destroy-method="close">
	
		<property name="url">
			<value>${}</value>
		</property>
		<property name="username">
			<value>${}</value>
		</property>
		<property name="password">
			<value>${}</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  class="">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
    <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类 
                  该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现-->
	<bean  class="">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事物控制 -->
	<bean  class="" 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=""/>
  
</beans>

spring配置文件中配置了数据源、spring jdbc、事务控制等等,另外采用了spring的注解配置,所以增加了最后两行。

3、配置spring mvc配置文件,文件默认位于web-info目录下,名称为中指定的servlet的名字
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:p="/schema/p" xmlns:context="/schema/context" xmlns:mvc="/schema/mvc" xsi:schemaLocation="
 /schema/beans
 /schema/beans/spring-beans-3.
 /schema/context
 /schema/context/spring-context-3.
 /schema/mvc
 /schema/mvc/spring-mvc-3.
 ">
 
     
	 <!-- 启用spring mvc 注解 <context:annotation-config />-->
	 <mvc:annotation-driven />
     

    <!-- 设置使用注解的类所在的jar包 -->
    <context:component-scan base-package=""/>
     
    
         <bean  class="">     
         <property name="exceptionMappings">     
             <props>     
                  <prop key="">error/error</prop>     
                  <prop key="">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  class="">
	 	<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 ;

import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

@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();
	    
	   // ("message", ());
		("message", "xxxxxxxxxxxxxxxx");
	    ("hello");
       
	    //int i=2/0;
	    
	    return mv;
    }
	
	@RequestMapping("/testJson") 
	public @ResponseBody App getJson(){
		
		App app=new App();
		("111");
		("xiaoming");
		return app;
	}


}

App类结构:
package ;

public class App {
	public String id;	//用户编号
	public String labelName;	//手机号
	
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		 = id;
	}
	public String getLabelName() {
		return labelName;
	}
	public void setLabelName(String labelName) {
		 = labelName;
	}
	
	
	
	
	
}

5、访问一般测试页面:
http://localhost:8080/monthReport/hello/tests
结果如下图:


测试json:
http://localhost:8080/monthReport/hello/testJson