整合ssh时出现问题,action接到了前台页面传过来的值,保存时没有报错,数据没有进数据库。

发布于 2021-11-19 06:43:45 字数 6930 浏览 832 评论 1

整合ssh时出现问题,action接到了前台页面传过来的值,保存时没有报错,数据没有进数据库。我用main方法测试保存功能是正常的。

applicationContext.xml配置

<bean id="dataSource" name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</value>
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource"><ref local="dataSource"/></property>
		<property name="mappingResources">
			<list>
				<value>cn/xiaoxing/h4/vo/UserModel.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.connection.pool_size">2</prop>
				<prop key="c3p0.min_size">5</prop>
				<prop key="c3p0.max_size">20</prop>
				<prop key="c3p0.timeout">180</prop>
				<prop key="c3p0.max_statements">5</prop>
			</props>
		</property>
	</bean>

	<bean name="h4Impl" class="cn.xiaoxing.h4.dao.impl.H4Impl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean name="userEbi" class="cn.xiaoxing.h4.business.ebo.UserEbo">
		<property name="userDao" ref="h4Impl"></property>
	</bean>
	
	<bean name="UserAction" class="cn.xiaoxing.h4.action.UserAction" scope="prototype">
		<property name="userEbi" ref="userEbi"></property>
	</bean>
	
	
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />  
	        <tx:method name="add*" propagation="REQUIRED" />  
	        <tx:method name="create*" propagation="REQUIRED" />  
	        <tx:method name="insert*" propagation="REQUIRED" />  
	        <tx:method name="update*" propagation="REQUIRED" />  
	        <tx:method name="merge*" propagation="REQUIRED" />  
	        <tx:method name="del*" propagation="REQUIRED" />  
	        <tx:method name="remove*" propagation="REQUIRED" />  
	        <tx:method name="put*" propagation="REQUIRED" />  
	        <tx:method name="use*" propagation="REQUIRED"/>  
	        <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
	        <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
	        <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
	        <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
	        <tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config expose-proxy="true">
		<aop:pointcut id="txPointCut" expression="execution(* cn.xiaoxing.h4..*.*(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
	</aop:config>
struts2配置
<struts>

	<constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<constant name="struts.ui.theme" value="simple"></constant>	
	<constant name="struts.objectFactory" value="spring"/>

	<package name="user" namespace="/user" extends="struts-default">
		<action name="user" class="UserAction">
			<result>/list.jsp</result>
		</action>
	</package>

</struts>
前台页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:form action="user" namespace="/user" method="post">
	<table width="200" border="1">
		<tr>
			<td>name</td>
			<td><s:textfield name="name"/></td>
		</tr>
		<tr>
			<td>password</td>
			<td><s:textfield name="pwd"/></td>
		</tr>
		<tr>
			<td colspan="2"><s:submit value="submit"/></td>
		</tr>
	</table>
</s:form>
</body>
</html>
后台实现
package cn.xiaoxing.h4.dao.impl;

import org.hibernate.SessionFactory;

import cn.xiaoxing.h4.dao.dao.UserDao;
import cn.xiaoxing.h4.vo.UserModel;

public class H4Impl implements UserDao{
	private SessionFactory sessionFactory = null;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	
	@Override
	public void create(UserModel um) {
		System.out.println("um-impl:"+um);
		sessionFactory.getCurrentSession().save(um);
	}

}

 action

package cn.xiaoxing.h4.action;

import cn.xiaoxing.h4.business.ebi.UserEbi;
import cn.xiaoxing.h4.vo.UserModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<UserModel>{

	private static final long serialVersionUID = -5543910820353786952L;

	private UserEbi userEbi = null;

	public void setUserEbi(UserEbi userEbi) {
		this.userEbi = userEbi;
	}

	private UserModel user = new UserModel();
	
	@Override
	public UserModel getModel() {
		return user;
	}
	

	@Override
	public String execute() throws Exception {
		System.out.println("um----------------------:" + user);
		this.userEbi.create(user);
		return super.execute();
	}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2021-11-19 14:37:08

将H4Impl类加:

@Component

@Transactional注解

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文