开发者社区> 问答> 正文

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

整合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&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();
	}


}

展开
收起
爱吃鱼的程序员 2020-06-22 19:23:27 504 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    将H4Impl类加:

    @Component

    @Transactional注解

    引用来自“-knight-”的答案

    将H4Impl类加:

    @Component

    @Transactional注解
    2020-06-22 19:23:43
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载