读书人

ssh整合有关问题

发布时间: 2011-12-26 23:09:58 作者: rapoo

ssh整合问题
我用的是spring2.5,hiernate3.2,struts1.2。做一个简单的用户注册时出问题了!
applicationContext.xml

XML code
<?xml version="1.0" encoding="GB18030"?><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.5.xsd">    <bean id="dataSource"        class="org.springframework.jndi.JndiObjectFactoryBean">        <property name="jndiName" value="java:comp/env/jdbc/bbs"></property>    </bean>    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource">            <ref bean="dataSource" />        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">                    org.hibernate.dialect.MySQLDialect                </prop>                <prop key="hibernate.connection.autocommit">true</prop>                <prop key="hibernate.show_sql">true</prop>            </props>        </property>        <property name="mappingResources">           <list>              <value>com/jun/bbs/domel/User.hbm.xml</value>           </list>        </property>    </bean>    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">        <property name="sessionFactory">           <ref bean="sessionFactory"/>        </property>    </bean>    <bean id="userdao" class="com.jun.bbs.dao.UserDao" abstract="true"></bean>    <bean id="userdaoimpl" class="com.jun.bbs.dao.impl.UserDaoImpl" parent="userdao">        <property name="hibernateTemplate">            <ref bean="hibernateTemplate"/>        </property>    </bean>    <bean name="/jsp/user" class="com.jun.bbs.struts.action.UserAction">            <property name="userdao">            <ref bean="userdaoimpl"/>         </property>    </bean>    </beans>

struts-config.xml
XML code
<?xml version="1.0" encoding="GB18030"?><!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="userForm" type="com.jun.bbs.struts.form.UserForm" />  </form-beans>  <global-exceptions />  <global-forwards />  <action-mappings>    <action      attribute="userForm"      input="/jsp/errors.jsp"      name="userForm"      parameter="status"      path="/jsp/user"      scope="request"      type="com.jun.bbs.struts.action.UserAction">      <forward name="registersuccess" path="/jsp/index.jsp"/>      <forward name="registerfailure" path="/jsp/register.jsp"/>      </action>  </action-mappings>  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>  <message-resources parameter="com.jun.bbs.struts.ApplicationResources" />  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">         <set-property property="pathnames" value="/WEB-INF/validator-rules.xml"/>  </plug-in>  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">    <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />  </plug-in></struts-config> 




问题是!能通过ActionForm中的验证!
Java code
public ActionErrors validate(ActionMapping mapping,            HttpServletRequest request) {        ActionErrors  errors = new ActionErrors();        if(type == 1) {            if(this.userid == null || "".equals(this.userid)) {                errors.add("userid",new ActionMessage("user.userid.null"));            }            if(this.userpwd == null || "".equals(this.userpwd)) {                errors.add("userques",new ActionMessage("user.userpwd.null"));            } else {                if(!(this.userpwd.equals(this.confirmpwd))) {                    errors.add("configpwd",new ActionMessage("user.confirmpwd.error"));                }            }            if(this.userques == null || "".equals(this.userques)) {                errors.add("userques", new ActionMessage("user.userques.null"));            }            if(this.userans == null || "".equals(this.userans)) {                errors.add("userans", new ActionMessage("user.userans.null"));            }            if(this.checkcode == null || "".equals(this.checkcode)) {                errors.add("checkcode",new ActionMessage("checkcode.null"));            }        }        return errors;    }


却不能进入到Action中去!
Java code
public ActionForward register(ActionMapping mapping, ActionForm form,            HttpServletRequest request, HttpServletResponse response) {        UserForm f = (UserForm) form;        String ccode = (String) request.getSession().getAttribute("ccode");        String checkcode = f.getCheckcode();        if (!(checkcode.equals(ccode))) {            ActionMessages errors = new ActionMessages();            errors.add("checkcode", new ActionMessage("checkcode.error"));            super.saveErrors(request, errors);            return mapping.getInputForward();        }        User user = null;        try {            user = this.userdao.queryByUserid(f.getUserid());            if(user == null) {               MD5Code md5 = new MD5Code();               user = new User();               user.setUserid(f.getUserid());               user.setUserpwd(md5.getMD5ofStr(f.getUserpwd()));               user.setUserques(f.getUserques());               user.setUserans(f.getUserans());               user.setSex("女");               user.setGrade(new Integer(1));               System.out.println("****"+f);                  this.userdao.register(user);               request.getSession().setAttribute("userid", user.getUserid());               request.getSession().setAttribute("grade", user.getGrade());               return mapping.findForward("registersuccess");            } else {                ActionMessages errors = new ActionMessages();                errors.add("exist",new ActionMessage("user.userid.exist"));                super.saveErrors(request, errors);                return mapping.getInputForward();            }        } catch (Exception e) {            e.printStackTrace();            return mapping.findForward("registerfailure");        }    }

控制台,没有报错!请大侠们,帮帮看看!
刚刚学完这三个框架。整合出现这问题!
没有任何提示异常,错误……
对了,用到了log4j!
谢谢!

[解决办法]
你的struts-config.xml 配置里面parameter="status"?
可你Action里面的方法名是register啊,
所以这里应该改成parameter="register"

------解决方案--------------------


楼上什么逻辑 那parameter搜索的是控件名好不?

楼主 你再看看控制台 或者刷新下看看有没有什么出错信息 把粘上来

你的代码是没什么问题的!


[解决办法]

探讨
你的struts-config.xml 配置里面parameter="status"?
可你Action里面的方法名是register啊,
所以这里应该改成parameter="register"

[解决办法]
sorry 看的不仔细 你的struts里的action 类型写错了 应该是type="org.springframework.web.struts.DelegatingActionProxy"
或者 你在web.XML里加个控制器<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
[解决办法]
探讨
引用:
你的struts-config.xml 配置里面parameter="status"?
可你Action里面的方法名是register啊,
所以这里应该改成parameter="register"


楼上,不好意思,你说错了,parameter="status"不是你说的意思

<action
attribute="userForm"
input="/jsp/errors.jsp"
name="userForm"
parameter="status" path="/jsp/user"
scope="request"


[解决办法]
探讨
是的,它的确是这个意思
不过我主要是为了反驳楼上那位仁兄很错误的认识!

[解决办法]
把你里面定义的那个<controller..>删了啊
[解决办法]
学习了
[解决办法]
是FormBean的验证方法有问题。
validate方法会根据你的返回值进跳转页面。

如果validate返回值不为null,那struts就认为有错误,转到input页面。如果validate方法的返回值为null,struts认为验证通过,那就会跳转到Action。

而你的validate方法无论验证通过没有都会返回一个ActionErrors对象,并不会在验证通过时返回一个空。

建议改成如下:

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(type == 1) {
if(this.userid == null || "".equals(this.userid)) {
errors.add("userid",new ActionMessage("user.userid.null"));
}
if(this.userpwd == null || "".equals(this.userpwd)) {
errors.add("userques",new ActionMessage("user.userpwd.null"));
} else {
if(!(this.userpwd.equals(this.confirmpwd))) {
errors.add("configpwd",new ActionMessage("user.confirmpwd.error"));
}
}
if(this.userques == null || "".equals(this.userques)) {
errors.add("userques", new ActionMessage("user.userques.null"));
}
if(this.userans == null || "".equals(this.userans)) {
errors.add("userans", new ActionMessage("user.userans.null"));
}
if(this.checkcode == null || "".equals(this.checkcode)) {
errors.add("checkcode",new ActionMessage("checkcode.null"));
}


}

if(errors.size()>0){
return errors;
}else{
return null;
}

}

[解决办法]
探讨
<?xml version="1.0" encoding="GB18030"?>
<!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="userForm" type="com.jun.bbs.struts.form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards /> …


[解决办法]
LZ的配置并没有错,请不要再误导楼主了。
[解决办法]
你的Action里面要重写exeute()方法,要用动态的action,需要用参数来传递吧。
你是用什么action?
[解决办法]
代码应该是没问题的,你不会真像上楼说的没有继承DispatchAction吧?

读书人网 >J2EE开发

热点推荐