如题,action调用数据访问层,插入数据时不报错也不能插入,查询方法值全部返回true。另,service层和dao层均通过单元测试,可以正常运行、操作。但是集成action和jsp就会出问题。
使用了 <property name="connection.autocommit">true</property>。无效。上代码:
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <tx:annotation-driven transaction-manager="transactionManager" />    <aop:aspectj-autoproxy proxy-target-class="true"/>  

<bean id="UserDAO" class="dao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" ></ref>
</property>
</bean>
<bean id="User_service" class="service.User_service" scope="prototype">
<property name="userDAO">
<ref bean="UserDAO" />
</property>
<bean id="register" class="Action.register" scope="prototype">
<property name="user_service">
<ref bean="User_service" />
</property>
</bean>
<bean id="login" class="Action.login" scope="prototype">
<property name="user_service">
<ref bean="User_service" />
</property>
</bean>
</beans>struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<package name="Light_Novel_System" extends="struts-default">
<action name = "register" class = "register">
<result name = "success">/login.jsp</result>
<result name = "error">/error.jsp</result>
<result name = "input">/register.jsp</result>
</action>
<action name = "login" class = "login">
<result name = "success">/welcom.jsp</result>
<result name = "error">/error.jsp</result>
</action>
</package></struts>    register.java
@Transactional(propagation = Propagation.REQUIRED)
public class register extends ActionSupport{ /**
 * 
 */
private static final long serialVersionUID = 1L;

private User_service user_service;
private String username;
private String psw;
private String psw2;
public static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
public String excecute(){
User user = new User();
Date date = new Date();
user_service = (User_service)factory.getBean("User_service");
Date yesteday = new Date(date.getTime() - 1000 * 60 * 60 * 24);
user.setName(username);
user.setPsw(psw);
user.setScoredTime(yesteday);
Boolean success = user_service.newUser(user);
if(success){
return "success";
}
else return "error";
}

public void validate(){
super.validate();
if("".equals(username) || username == null){
this.addFieldError("user_name", this.getText("username_null"));
}
if("".equals(psw) || psw == null){
this.addFieldError("user_psw", this.getText("psw_null"));
}
if("".equals(psw2) || psw2 == null){
this.addFieldError("user_psw2", this.getText("psw2_null"));
}
if(!psw2.equals(psw)){
this.addFieldError("user_psw2", this.getText("psw2_not_equal"));
}
}

public void setUser_service(User_service user_service) {
this.user_service = user_service;
}
public User_service getUser_service() {
return user_service;
} public void setPsw2(String psw2) {
this.psw2 = psw2;
} public String getPsw2() {
return psw2;
} public void setUsername(String username) {
this.username = username;
} public String getUsername() {
return username;
} public void setPsw(String psw) {
this.psw = psw;
} public String getPsw() {
return psw;
}
}跪求解答!!不胜感激!!!!

解决方案 »

  1.   

    问题补充:beans获取没有问题,获取方式都试过了。
      

  2.   

    收先 页面 <s:form  s_add>  <input></input> <s:form>strus2提交方式
    再就是 struts.xml 打短点 跟中一下 看执行没执行进累 里 执行进了 说明 xml 对了,其次看连接
      

  3.   

    页面没有问题,跳转没有问题,action运行也没有问题,就是调用service层函数的时候不访问数据库。而单独对service层函数单元测试也没有问题……
      

  4.   

    问题有新进展了!我对action做了单元测试,发现register的action里调用userservice对象的newuser方法时,没有向数据库添加新元素。而在test类中,用register对象get出其中的userservice对象后,又能够正常的使用这个对象中的addnewuser方法向数据库中添加。原因不明。跪求解答!
      

  5.   

    action调用数据访问层,插入数据时不报错也不能插入,查询方法值全部返回true。另,service层和dao层均通过单元测试,可以正常运行、操作。但是集成action和jsp就会出问题。会出现什么问题!你要把问题描述在详细一点就好了!
      

  6.   

    问题定位更清晰了,从log文件里查到,在action里虽然获取到了service的对象(userservice),但是当调用这个对象内部的方法插入user时,并没有生成hql语句,而是直接跳过了这个方法。在测试类中直接get到action的userservice对象调用newuser就能够生成hql,说明对象已经获取到了,但是在action的内部无法完成调用命令,请教各位大侠这是为何?!
      

  7.   

    问题定位更新!发现action的execute方法中,能获取到spring分配的bean,但是确不能调用这些bean中的方法,可以事后从别的类中get到这些bean的对象,并调用方法,可是在action中却做不到
      

  8.   

    在你的插入语句的地方,加断点看下,并且打印sql看看。
      

  9.   

    你打印一下,你能从jsp页面获得数据吗