现在springMVC的整合吧
第一步:web.xml的配置
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
dwr的配置()
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.spring.DwrSpringServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
第二步:WEB-INF/spring-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义一个视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> </beans>
第三步: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" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 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.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 激活spring的注解. --> <context:annotation-config /> <!-- 扫描注解组件并且自动的注入spring beans中. 例如,他会扫描@Controller 和@Service下的文件.所以确保此base-package设置正确. --> <context:component-scan base-package="net.rytong" /> <!-- 配置注解驱动的Spring MVC Controller 的编程模型.注:次标签只在 Spring MVC工作! --> <mvc:annotation-driven /> <mvc:default-servlet-handler default-servlet-name="IndexServlet"/> <!-- ********************************************** --> <!-- dwr启用 annotation 配置模式 --> <dwr:configuration> <dwr:convert type="bean" class="net.rytong.entity.Person" /> </dwr:configuration> <!-- pring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类 --> <dwr:annotation-config /> <!-- 开启debug --> <dwr:controller id="dwrController" debug="true" /> <!-- ********************************************** --> <!-- 导入jdbc的配置文件 --> <import resource="jdbc-context.xml" /> </beans>
第四步:数据源jdbc-context.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: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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- <context:property-placeholder location="classpath:db.properties" /> --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="${jdbc.driverClassName}" /> <property name="driverUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maximumConnectionCount" value="300" /> <property name="minimumConnectionCount" value="10" /> <property name="prototypeCount" value="4" /> <property name="maximumActiveTime" value="600000" /> <property name="simultaneousBuildThrottle" value="5" /> <property name="testBeforeUse" value="true" /> <property name="houseKeepingTestSql" value="SELECT CURRENT_DATE" /> </bean> <!-- 定义jdbc配置信息路径 --> <context:property-placeholder location="/WEB-INF/db.properties" /> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 定义事务管理 --> <!-- See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> </beans>
dao的代码:
@Repository("personDao") public class PersonDao { private SimpleJdbcTemplate jdbcTemplate; @Resource(name="dataSource") public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new SimpleJdbcTemplate(dataSource); } public List<Person> getAllPerson() { String sql = "select id, first_name, last_name, money from person"; // Maps a SQL result to a Java object RowMapper<Person> mapper = new RowMapper<Person>() { public Person mapRow(ResultSet rs, int rowNum) throws SQLException { Person person = new Person(); person.setId(rs.getInt("id")); person.setFirstName(rs.getString("first_name")); person.setLastName(rs.getString("last_name")); person.setMoney(rs.getDouble("money")); return person; } }; return jdbcTemplate.query(sql, mapper); } }
service中的代码:
@Service("personService") @RemoteProxy(name="PersonService") public class PersonService { protected static Logger logger = Logger.getLogger("service"); @Autowired private PersonDao personDao; /** *检索所有的Person */ @RemoteMethod public List<Person> getAll() { logger.debug("Retrieving all persons"); System.out.println(personDao+"=========="); return personDao.getAllPerson(); } }
control中的代码:
@Controller @RequestMapping("/main") public class MainController { protected static Logger logger = Logger.getLogger("controller"); @Resource(name="personService") private PersonService personService; /** *获得所有的Person并返回到指定JSP页面 * * @return the name of the JSP page */ @RequestMapping(value = "/persons.do", method = RequestMethod.GET) public String getPersons(Model model) { logger.debug("Received request to show all persons"); // 调用personService中的getAll获得所有的Person List<Person> persons = personService.getAll(); // 把Person装入一个指定的model model.addAttribute("persons", persons); // 解析 /WEB-INF/jsp/personspage.jsp return "personspage"; }
要想servlet要spring容器来管理,在web.xml中 去掉springMVC配置 加上如下配置:
<servlet> <servlet-name>UserLoginServlet</servlet-name> <servlet-class>net.rytong.servlet.ServletProxy</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginServlet</servlet-name> <url-pattern>/UserLoginServlet</url-pattern> </servlet-mapping>
并加上这个代理类
@SuppressWarnings("serial") public class ServletProxy extends GenericServlet { private Servlet proxy; @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { proxy.service(req, res); } @Override public void init() throws ServletException { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); System.out.println(getServletName()+"==============="); this.proxy = (Servlet) wac.getBean(getServletName()); proxy.init(getServletConfig()); } }
具体代码下载:( 数据库是基于mysql,jar包管理是Maven 下载后报错请修改pom.xml)