论坛首页 入门讨论版 Struts

表单提交数据的问题

浏览 339 次
该帖已经被评为新手帖
作者 正文
最后更新时间:2008-03-12
问题如下:
有两个页面:userLogin.jsp和userReg.jsp
处理分别是调用一个Action中的两个方法
现在的问题是 userLogin.jsp中表单提交的username和password都是正确的,
但是userReg.jsp提交的到了Action中就会变
比如我提交 username = test / password = test
那么在userLogin.jsp要使用到的方法中能得到正确的username 和 password
但是在 userReg.jsp要使用的Action方法中 却得到一些奇怪的值,如果什么都不输入的话,会得到http://localhost:8080......等等一串东西

我把配置文件贴一下,请大大们看看 ,有什么问题和要改进的地方,谢谢!!!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
	<!-- 用于初始化Spring容器的Listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 定义Struts2的FilterDispathcer的Filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>


struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="objectFactory" value="spring"></constant>
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<package name="struts2" extends="struts-default">
		<action name="userLogin" class="com.test.action.LoginAction" method="login">
			<result name="success">userLoginSuc.jsp</result>
			<result name="input">userLogin.jsp</result>
		</action>
		<action name="userSignup" class="com.test.action.LoginAction" method="signUp">
			<result name="success">userLoginSuc.jsp</result>
			<result name="input">userReg.jsp</result>
		</action>
	</package>	
</struts>


applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<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.xsd">

	<!-- 定义数据源Bean,使用C3P0数据源实现 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="root"/>
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="12345"/>
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="20"/>
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
		<property name="maxIdleTime" value="20"/>
	</bean>

    <!--定义了Hibernate的SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
				<value>User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.jdbc.batch_size">20</prop> 
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

	<!-- 事务管理 -->
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
	    <!--  事务拦截器bean需要依赖注入一个事务管理器 -->
        <property name="transactionManager" ref="transactionManager"/>
    	<property name="transactionAttributes">
		    <!--  下面定义事务传播属性-->
		    <props>
			    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
		    	<prop key="*">PROPAGATION_REQUIRED</prop>
		    </props>
	    </property>
	</bean>

    <!-- 定义BeanNameAutoProxyCreator-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	    <!--  指定对满足哪些bean name的bean自动生成业务代理 -->
	    <property name="beanNames">
            <!--  下面是所有需要自动创建事务代理的bean-->
            <list>
                <value>userManager</value>
            </list>
            <!--  此处可增加其他需要自动创建事务代理的bean-->
	    </property>
        <!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
        <property name="interceptorNames">
            <list>
                <!-- 此处可增加其他新的Interceptor -->
                <value>transactionInterceptor</value> 
            </list>
        </property>
    </bean>

    <bean id="userManager" class="com.test.service.impl.UserManagerImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

	<bean id="userDao" class="com.test.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<!-- action 注入 -->
	<bean id="loginAction" class="om.test.action.LoginAction" scope="prototype" autowire="byName">  
        <property name="userManager">  
            <ref bean="userManager" />  
        </property>  
    </bean>
</beans>

   
最后更新时间:2008-03-12
你的问题出在哪我没看出来.

不过我记得用spring的话应该把struts2的action交给spring容器来管理,不是吗?

那么struts.xml中定义<action class=""> 这里应该写spring容器中bean的id.

难道像这样直接写action类名也可以使用spring的注入吗?
   
0 请登录后投票
最后更新时间:2008-03-12
struts.xml 中 <action class=""> 写spring容器中bean的id的话,那不是有两个重名的action了?
   
0 请登录后投票
最后更新时间:2008-03-12
yyjn12说得对。既然使用了Spring做容器,那么在struts.xml中各个action的class属性指定为applicationContext.xml中定义的bean的id好了

不同的action可以引用同一个bean的,为它们的method属性指定不同的方法名就可以了(如果两个action引用同一个bean且method属性也相同,那么一个action是另一个的别名)
   
0 请登录后投票
最后更新时间:2008-03-13
恩 测试了一下
yyjn12 和 movingboy 说的对
修改后可以得到正确的值了
   
0 请登录后投票
论坛首页 入门讨论版 Struts

跳转论坛:
JavaEye推荐