主要对上一篇Struts2&Spring整合的改造
简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本
src/bean.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 --> <!-- 引入其他配置文件 --> <import resource="config/bean-action.xml"/>
<import resource="config/bean-dao.xml"/>
<import resource="config/bean-service.xml"/>
<import resource="config/bean-entity.xml"/> </beans>
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"> <!-- 1. struts配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 2. spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <error-page>
<error-code>404</error-code>
<location>/notFoundError.jsp</location>
</error-page>
</web-app>
bean-action.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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Action中需要注入Service -->
<bean id="doLogin" class="com.loaderman.crm.action.DoLoginAction" scope="prototype">
<property name="accountService" ref="accountService"></property>
<property name="account" ref="account"></property>
</bean>
<bean id="addPolicy" class="com.loaderman.crm.action.AddPolicyAciton" scope="prototype">
<property name="policyService" ref="policyService"></property>
<property name="policy" ref="policy"></property>
</bean>
<bean id="addUser" class="com.loaderman.crm.action.AddUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
<property name="user" ref="user"></property> </bean>
<bean id="delUser" class="com.loaderman.crm.action.DelUserAciton" scope="prototype">
<property name="userService" ref="userService"></property>
<property name="user" ref="user"></property>
</bean>
<bean id="delPolicy" class="com.loaderman.crm.action.DelPolicyAciton" scope="prototype">
<property name="policyService" ref="policyService"></property>
<property name="policy" ref="policy"></property>
</bean>
<bean id="gtPolicyList" class="com.loaderman.crm.action.GetPolicyListAciton" scope="prototype">
<property name="policyService" ref="policyService"></property>
</bean>
<bean id="getUserList" class="com.loaderman.crm.action.GetUserListAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> </beans>
bean-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao" class="com.loaderman.crm.dao.impl.AccountDaoimp" scope="singleton" lazy-init="false">
</bean>
<bean id="policyDao" class="com.loaderman.crm.dao.impl.PolicyDaoimp" scope="singleton" lazy-init="false">
</bean>
<bean id="userDao" class="com.loaderman.crm.dao.impl.UserDaoimp" scope="singleton" lazy-init="false">
</bean> </beans>
bean-entity.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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="account" class="com.loaderman.crm.entity.Account" scope="prototype" lazy-init="false">
</bean>
<bean id="user" class="com.loaderman.crm.entity.User" scope="prototype" lazy-init="false">
</bean>
<bean id="policy" class="com.loaderman.crm.entity.Policy" scope="prototype" lazy-init="false">
</bean> </beans>
bean-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
"> <bean id="accountService" class="com.loaderman.crm.service.impl.AccountServiceimp">
<property name="accountDao" ref="accountDao"></property> </bean>
<bean id="policyService" class="com.loaderman.crm.service.impl.PolicyServiceimp">
<property name="policyDao" ref="policyDao"></property>
</bean>
<bean id="userService" class="com.loaderman.crm.service.impl.UserServiceimp">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
请求接口演示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>用户登录</title>
<link href="${pageContext.request.contextPath }/css/common.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function validate_form() {
var name = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (name == null || name == "") {
alert("姓名不能为空");
return false;
}
if (password == null || password == "") {
alert("密码不能为空");
return false;
}
return true; } </script>
</head>
<body>
<h2>用户登录</h2> <p><span
id="loginHit"> <%=session.getAttribute("msg") == null ? "" : session.getAttribute("msg") %><% session.removeAttribute("msg"); %></span>
</p> <form action="${pageContext.request.contextPath }/doLogin" onsubmit="return validate_form()" method="post"> <table class="loginTable"> <tr>
<td id="name">用户名:</td>
<td id="nmtd"><input type="text" name="username" id="username" value="admin"></td>
</tr> <tr>
<td id="pwd">密 码:</td>
<td id="pwdtd"><input type="password" name="password" id="password" value="admin"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table> </form> </body>
</html>
点击源码下载