1首先看你的配置文件对否?
我写了一个用户注册的页面 regist.jsp 放在项目根目录下的form文件里面
代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for RegistForm form</title>
</head>
<body>
<html:form action="/regist.do">
用户名 : <html:text property="username"/>
<html:errors property="username"/><br/>
密 码 : <html:password property="password"/>
<html:errors property="password"/><br/>
<html:submit value="注册"/>
<html:cancel value="取消"/>
</html:form>
</body>
</html>
2写一个跳转页面 success.jsp和failed.jsp
3 先搭建struts框架 希望你搭建过,成功后
4 搭建hibernate框架 (注意不要选默认的添加jar包,选择复制jar包) 我用的mysql数据库。
在mysql数据库school下面建表 user
sql语句如下
CREATE TABLE `user` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=latin1;
5. 用hibernate连接数据库。
hibernate.cfg.xml文件如下
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">zhxb</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/school
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">
mysql_connection
</property>
<property name="connection.password">zhxb</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- <mapping resource="hibernatedao/User.hbm.xml" /> -->
<mapping resource="springdao/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
6 搭建spring框架
如何搭建,自己到网上找,我只说错误发生的地方和整合的地方,注意要选择复制jar包,搭建好后,
进行strut和spring的整合。
修改struts-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="registForm" type="com.struts.form.RegistForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/form/regist.jsp"
name="registForm"
path="/regist" (注意路径)
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">(注意这里是代理类,并不是真正的action类)
<forward name="failed" path="/failed.jsp" />
<forward name="success" path="/success.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> (这个地方路径要正确)
</plug-in>
</struts-config>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean name="/regist" class="com.struts.action.RegistAction">
<property name="message">
<value>spring整合sturts成功!</value>
</property>
<property name="userdao">
<ref bean="UserDAOProxy"></ref></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="man" name="man_name" class="springtest.Man">
<property name="message">
<value>你好!</value>
</property>
</bean>
<bean id="UserDAO" class="springdao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<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="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
action 类中execute的代码如下
public class RegistAction extends Action {
/*
* Generated Methods
*/
private UserDAO userdao;
private String message="struts action is execute";
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
RegistForm registForm = (RegistForm) form;// TODO Auto-generated method stub
System.out.print(getMessage());
User user=new User();
user.setUsername(registForm.getUsername());
user.setPassword(registForm.getPassword());
UserDAO userdao=getUserdao();
userdao.save(user);
/*
//开启事务
Transaction tran=userdao.getSession().beginTransaction();
userdao.save(user);
//关闭事务
tran.commit();
//显示记录
java.util.List<User> list=userdao.findAll();
for(User o: list)
{
System.out.print("用户名"+o.getUsername()+" ");
System.out.println("密码"+o.getPassword());
}
*/
return mapping.findForward("success");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public UserDAO getUserdao() {
return userdao;
}
public void setUserdao(UserDAO userdao) {
this.userdao = userdao;
}
}
发生错误的地方如下
出现404错误如 action is not avilable 如果前面都没问题可能是包问题 看你工程下面有 asm-2.2.3.jar 没有,如果有就删掉它 ,和asm.ja包冲突。
出现500错误或404错误, 如servelt action is not avliable 时
删除项目下面的hibernate-annotations.jar包。
这是我搭建ssh框架遇到的问题, 和大家分享一下。希望对你们有帮助。