ssh整合问题 从一个action访问另一个action报错
这两天练习ssh整合,做了一个小例子遇到了问题。
环境: myeclipse8.6 + tomcat7
整合的例子是 struts2.1 + spring 2.0 + hibernate3.3
所有的支持时用myeclipse的可视化工具添加的。
看配置文件:
struts.xml
- XML code
<package name="suibiaojiao" extends="struts-default" namespace="/"> <action name="login" class="loginAction"> <result name="ok">findAll.action</result> <result name="error">/error.jsp</result> </action> <action name="findAll" class="findAllAction"> <result name="ok">/studentList.jsp</result> <result name="error">/error.jsp</result> </action></package>
applicationContext.xml
- XML code
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://127.0.0.1:3306"></property> <property name="username" value="root"></property> <property name="password" value="lm"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="studentDAO" class="com.lm.Impdao.StudentDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="stuBiz" class="com.lm.Impbiz.StudentBiz"> <property name="stuDAO" ref="studentDAO"></property> </bean> <bean id="loginAction" class="com.lm.struts2.loginAction"> <property name="stuBiz" ref="stuBiz"></property> </bean> <bean id="findAllAction" class="com.lm.struts2.findAllAction"> <property name="stuBiz" ref="stuBiz"></property> </bean> </beans>
loginAction.java
- Java code
package com.lm.struts2;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.lm.biz.IStudentBiz;import com.lm.dao.Student;import com.opensymphony.xwork2.ActionSupport;public class loginAction extends ActionSupport { private Student stu; private IStudentBiz stuBiz; public void setStuBiz(IStudentBiz stuBiz) { this.stuBiz = stuBiz; } public Student getStu() { return stu; } public void setStu(Student stu) { this.stu = stu; } public String execute() throws Exception{ HttpServletRequest request=ServletActionContext.getRequest(); HttpSession session=request.getSession(); System.out.println("login"); String tag="error"; if(stuBiz.login(stu)) tag="ok"; return tag; }}
findAllAction.java
- Java code
package com.lm.struts2;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.lm.biz.IStudentBiz;import com.lm.dao.Student;import com.opensymphony.xwork2.ActionSupport;public class findAllAction extends ActionSupport { private IStudentBiz stuBiz; public void setStuBiz(IStudentBiz stuBiz) { this.stuBiz = stuBiz; } public String execute() throws Exception{ HttpServletRequest request=ServletActionContext.getRequest(); HttpSession session=request.getSession(); System.out.println("hah"); String tag="error"; try{ List<Student> stus = stuBiz.findAll(); tag="ok"; request.setAttribute("stus", stus); }catch(Exception ex){ ex.printStackTrace(); } return tag; }}login.jsp:
- HTML code
<form action="<%=request.getContextPath() %>/login.action">用户名:<input name="stu.stuName"><br>密码:<input name="stu.stuPwd"><br><input type="submit" value="submit"></form>
逻辑是这样的:
在login.jsp页面中输入用户名跟密码,把数据提交至loginAction,然后判断数据库中是否存在该用户,如果存在该用户接着访问findAllAction然后通过一个页面(studentList.jsp)把所有用户的信息显示出来,如果不存在则通过另一个页面(error.jsp)给出出错提示。
数据库访问这块是没有问题的,能进行正常的数据操作。数据提交至loginAction也是正常的,可问题是如果输入的用户名密码正确按理说应该访问findAllAction可是这时候页面报出异常:
HTTP Status 404 - /ssh4/findAll.action
--------------------------------------------
type Status report
message /ssh4/findAll.action
description The requested resource (/ssh4/findAll.action) is not available.
意思是找不到findAllAction,但是我仔细检查了下配置文件(如上的给出)没有发现什么问题啊,而且如果我把login.jsp页面
的action改成action="<%=request.getContextPath() %>/findAll.action",这个时候findAllAction是能正常访问的(依据是studenList.jsp能正常显示数据信息)。
那为什么通过loginAction再访问findAllAction就出错了呢。。。。帮忙解决下,谢谢大家~
[解决办法]
action重定向下应该就可以了吧
<action name="login" class="loginAction">
<result name="ok" type="redirect"> findAll.action</result>
<result name="error">/error.jsp</result>
</action>