1. web.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>Dispatcher</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>Dispatcher</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- <filter>
- <filter-name>SetCharacterEncoding</filter-name>
- <filter-class>
- com.lz.filter.SetCharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>SetCharacterEncoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
2. 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:tx="http://www.springframework.org/schema/tx"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/jee
- http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <context:component-scan base-package="com.lz" />
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass">
- <value>org.springframework.web.servlet.view.JstlView</value>
- </property>
- <property name="prefix">
- <value>/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </bean>
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="oracle.jdbc.driver.OracleDriver">
- </property>
- <property name="url"
- value="">
- </property>
- <property name="username" value=""></property>
- <property name="password" value=""></property>
- </bean>
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="find*" read-only="true"></tx:method>
- <tx:method name="*"></tx:method>
- </tx:attributes>
- </tx:advice>
- <aop:config proxy-target-class="true">
- <aop:advisor
- pointcut="execution(* com.lz.service.impl.*Impl.*(..))"
- advice-ref="txAdvice"></aop:advisor>
- </aop:config>
- </beans>
3. JdbcBaseDao.java
- package com.lz.dao.base;
- import javax.annotation.Resource;
- import javax.sql.DataSource;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- public class JdbcBaseDao extends JdbcDaoSupport {
- @Resource(name = "dataSource")
- public void setSuperDataSource(DataSource dataSource) {
- super.setDataSource(dataSource);
- }
- }
4. TestDao.java
- package com.lz.dao;
- import java.util.List;
- import org.springframework.stereotype.Repository;
- import com.lz.dao.base.JdbcBaseDao;
- @Repository("testDao")
- public class TestDao extends JdbcBaseDao {
- @SuppressWarnings("unchecked")
- public List find() {
- return getJdbcTemplate().queryForList("SELECT * FROM TEST");
- }
- }
5. TestController.java
- package com.lz.web;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.lz.dao.TestDao;
- @Controller
- public class TestController {
- @Autowired
- private TestDao testDao;
- private String formView = "test";
- @RequestMapping("/test.html")
- public String view(HttpServletRequest request) {
- request.setAttribute("list", testDao.find());
- return formView;
- }
- }
5. index.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form name="form1" method="post" action="test.html">
- <input type="submit" name="Submit" value="提交">
- </form>
- </body>
- </html>
6. test.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
- <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <table>
- <c:forEach var="list" items="${list}">
- <tr>
- <td>
- ${list.TEST_ID}
- </td>
- <td>
- ${list.TEST_NAME}
- </td>
- </tr>
- </c:forEach>
- </table>
- </body>
- </html>
浙公网安备 33010602011771号