Struts+Spring+Hibernate练习(完整)转载
工具:
Eclipse3.1、MyEclipse4.03、Tomcat5.5.9、Properties Editor插件、MySql4.1.13
新建工程:名称为 login
创建Struts框架
创建 index.jsp,增加一链接指向 login.jsp
按下Ctrl + N,创建 login.jsp、LoginAction,使用MyEclipse的向导就可以了,
记得选对正确的版本
在ActionForm配置页中选择类型为动态Form,并继承于DynaValidatorForm,新增两个属性:username、password,在创建jsp文件打上钩,将路径改为/login.jsp,然后下一步,改LoginAction的Input source改为/login.jsp,点击完成
按下Ctrl + N 创建一个forwards,
记得选对正确的版本
name 输入 indexGo
路径选择 /index.jsp
配置validator
先添加Struts插件,使用向导
Plugin class : org.apache.struts.validator.ValidatorPlugIn
Property : pathnames
Value : /WEB-INF/validator-rules.xml,/WEB-INF/validation.xml
这里需要两个xml文件
现在创建“ validation.xml ” 文件
在这里说明一点,我使用MyEclipse创建的Struts框架中缺少了validator-rules.xml文件,需要动拷贝到WEB-INF目录中
此文件可以到 http://struts.apache.org/下载
文件内容如下:
<
form-validation
>
< formset >
< form name ="loginForm" >
< field property ="username" depends ="required" >
< arg0 key ="prompt.username" />
</ field >
< field property ="password" depends ="required" >
< arg0 key ="prompt.password" />
</ field >
</ form >
</ formset >
</ form-validation >
< formset >
< form name ="loginForm" >
< field property ="username" depends ="required" >
< arg0 key ="prompt.username" />
</ field >
< field property ="password" depends ="required" >
< arg0 key ="prompt.password" />
</ field >
</ form >
</ formset >
</ form-validation >
编辑资源文件“ApplicationResources.properties”
增加以下内容
prompt.username=User Name
prompt.password=User Password
prompt.password=User Password
errors.required={0} is required.
再创建中文件资源文件“ApplicationResources_zh_CN.properties”
增加以下内容
prompt.username=用户名称
prompt.password=登录密码
prompt.password=登录密码
errors.required={0} 必需填写!
修改struts-config.xml文件
在以下位置增加绿色字体部份
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="com.test.struts.action.LoginAction" />
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="com.test.struts.action.LoginAction" />
</action-mappings>
这里说明提交的数据必需经过验证,而验证则是通过validator框架进行的。
修改LoginAction.java文件的execute方法,内容如下
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
String username=loginForm.getString("username");
String password=loginForm.getString("password");
if(username.equals("test")||password.equals("test")){
return mapping.findForward("indexGo");
}else{
return mapping.getInputForward();
}
}
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
String username=loginForm.getString("username");
String password=loginForm.getString("password");
if(username.equals("test")||password.equals("test")){
return mapping.findForward("indexGo");
}else{
return mapping.getInputForward();
}
}
现在再修改一下login.jsp
增加以下绿色字体部份
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
其中 charset=UTF-8 是使用UTF-8的字符编码,这也是为了支持国际化而使用的。
好了,现在可以启动Tomcat进行测试了
http://localhost/login/ 这里说明一下,我的Tomcat已经装端口号改为80了,所以就不必使用 http://localhost:8080/login/这样的方法了。
如果不输入任何数据而直接提交表单的话就可以看到效果了。
好了,如果没有什么问题的话就继续往下看吧,如果有问题的话就得往上看了^_^
现在创建Spring框架了,在这里我将Spring所有的包全部加载进去,因为我还不知道具体用到哪些类,全部加进去方便点
单选框选第二个,这样的话所有的类库和标签等都将拷贝到项目中去,这样方便以后的布署
下一步后是创建配置文件,将文件放到“WebRoot/WEB-INF”目录下,文件名称为“applicationContext.xml”
配置struts-config.xml文件,添加(spring)的插件
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
修改LoginAction配置
原:
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="com.test.struts.action.LoginAction" />
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="com.test.struts.action.LoginAction" />
</action-mappings>
改为:
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="org.springframework.web.struts.DelegatingActionProxy" />
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
绿色字体部份为修改内容
这里将使用spring的代理器来对Action进行控制
当提交到/login.do是将控制权交给了spring,然后由spring来决定是否转回到struts的Action
现在来配置spring
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
<beans>
<bean name="/login" class="com.test.struts.action.LoginAction" singleton="false"></bean>
<bean name="/login" class="com.test.struts.action.LoginAction" singleton="false"></bean>
</beans>
绿色字体是关于转交控制权的配置内容
属性singleton="false",指明了Action 的实例获取方式为每次重新创建。解决了Struts中令人诟病的线程安全问题(Struts中,由一个Action实例处理所有的请求,这就导致了类公用资源在并发请求中的线程同步问题。)(摘自spring开发指南)
这时如果你要进行测试也是可以的,不过为了省点时间就不进行测试了。
建立数据库在 这里我使用的是mysql4.1.13
CREATE TABLE `user` (
`ID` int(11) NOT NULL auto_increment,
`USERNAME` varchar(50) NOT NULL default '',
`PASSWORD` varchar(50) NOT NULL default '',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
`ID` int(11) NOT NULL auto_increment,
`USERNAME` varchar(50) NOT NULL default '',
`PASSWORD` varchar(50) NOT NULL default '',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
添加记录 insert into user (USERNAME,PASSWORD) values ('test','test')
创建Hibernate框架
在配置界面中配置数据库的连接部份,重要的是点击链接将jdbc拷贝到lib目录中
使用MyEclipse的数据Database Explorer工具创建User.hmb.xml、AbstractUser.java、User.java映射文件
创建完成后可以将自动生成的hibernate.cfg.xml删除
创建UserDAO.java、UserDAOImp.java
UserDAO.java
public interface UserDAO {
public abstract boolean isValidUser(String username, String password);
}
UserDAOImp.java
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.test.Hibernate.SessionFactory;
public class UserDAOImp extends HibernateDaoSupport implements UserDAO {
private SessionFactory sessionFactory;
private static String hql = "from User u where u.username=? ";
public boolean isValidUser(String username, String password) {
List userList = this.getHibernateTemplate().find(hql, username);
if (userList.size() > 0) {
return true;
}
return false;
}
}
修改LoginAction.java文件,使用userDao的方法来进行用户验证
package com.test.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import com.test.UserDAO;
public class LoginAction extends Action {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
this.userDAO = userDAO;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
// TODO Auto-generated method stub
String username = (String) loginForm.get("username");
String password = (String) loginForm.get("password");
loginForm.set("password", null);
if (userDAO.isValidUser(username,password)) {
return mapping.findForward("indexGo");
} else {
return mapping.getInputForward();
}
}
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
// TODO Auto-generated method stub
String username = (String) loginForm.get("username");
String password = (String) loginForm.get("password");
loginForm.set("password", null);
if (userDAO.isValidUser(username,password)) {
return mapping.findForward("indexGo");
} else {
return mapping.getInputForward();
}
}
}
绿色字体为修改部份
绿色字体为修改部份
现在剩下最后的spring配置了
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost/test </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > root </ value >
</ property >
</ bean >
<!-- 配置sessionFactory, 注意这里引入的包的不同 -->
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="mappingResources" >
< list >
< value > com/test/Hibernate/User.hbm.xml </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
< prop key ="hibernate.show_sql" > true </ prop >
</ props >
</ property >
</ bean >
< bean id ="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref local ="sessionFactory" />
</ property >
</ bean >
< bean id ="userDAO" class ="com.test.UserDAOImp" >
< property name ="sessionFactory" >
< ref local ="sessionFactory" />
</ property >
</ bean >
< bean id ="userDAOProxy" class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="target" >
< ref local ="userDAO" />
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="insert*" > PROPAGATION_REQUIRED </ prop >
< prop key ="get*" > PROPAGATION_REQUIRED,readOnly </ prop >
< prop key ="is*" > PROPAGATION_REQUIRED,readOnly </ prop >
</ props >
</ property >
</ bean >
< bean name ="/login" class ="com.test.struts.action.LoginAction" singleton ="false" >
< property name ="userDAO" >
< ref bean ="userDAOProxy" />
</ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost/test </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > root </ value >
</ property >
</ bean >
<!-- 配置sessionFactory, 注意这里引入的包的不同 -->
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="mappingResources" >
< list >
< value > com/test/Hibernate/User.hbm.xml </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
< prop key ="hibernate.show_sql" > true </ prop >
</ props >
</ property >
</ bean >
< bean id ="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref local ="sessionFactory" />
</ property >
</ bean >
< bean id ="userDAO" class ="com.test.UserDAOImp" >
< property name ="sessionFactory" >
< ref local ="sessionFactory" />
</ property >
</ bean >
< bean id ="userDAOProxy" class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="target" >
< ref local ="userDAO" />
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="insert*" > PROPAGATION_REQUIRED </ prop >
< prop key ="get*" > PROPAGATION_REQUIRED,readOnly </ prop >
< prop key ="is*" > PROPAGATION_REQUIRED,readOnly </ prop >
</ props >
</ property >
</ bean >
< bean name ="/login" class ="com.test.struts.action.LoginAction" singleton ="false" >
< property name ="userDAO" >
< ref bean ="userDAOProxy" />
</ property >
</ bean >
</ beans >
现在可以进行测试了!
在编写代码有配置内容时一定要注意 hibernate 和 hibernate3 ,这两个包的名字就只差一个字,千万不要有错,否则找错误可是很难的。
|
|