公司以前的一个项目,开发是用struts1开发的,现在要增加一个新的模块,根据登录进去的用户的session取出username,根据用户的身份来得到一张待办事件的表,我的思路是在登录加载页面的时候,利用js调用一个函数,直接onload过去,传过去的是一个action请求,没有任何参数,因为在登录的时候,LoginAction已进行request.getSession().setAttribute(Globals.USER_KEY, su);操作了,我在我的action中直接取出su.getusername在进行操作即可, 这个是我的思路,现在的问题上我还没有进入action,在我自己的action类上面加了断点,压根就没进去,求各位高手帮帮忙,小弟谢过了。一下为代码:struts-congig.xml;(由于不需要表单验证,没有form-bean配置)
<!--新增加的模块!功能是登陆进去根据登录用户的session取出用户的相应信息 -->
<action path="/newtask" scope="request" validate="false"
type="com.coreram.framework.common.action.NewTask" parameter="method" >
<forward name="ok" path="/success.jsp"/>
</action>
我自己的action类,只是为了测试,里面的代码基本可以无视:package com.coreram.framework.common.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.DispatchActionSupport;public class NewTask extends DispatchActionSupport{ public ActionForward newtask(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { if ("tom".equals("tom")) {
return mapping.findForward("ok");
}
return null;
}
}javascript:function doNewsTask(){
//task.jsp一开始加载首先调用这个函数,把action传过去,获取到当前session的username!!!
try { //alert("error");
//location.href ="/Test.jsp";这种写法是正确的!!
//window.location.href="/Test.jsp"; 这种写法是错误的!!
var url="/newtask.do";
window.location.href=url;
}
catch(e)
{alert("wrong");}
}
另外struts-congig.xml中的全局异常节点使用了一个异常处理类,因为程序不是我写的,我是新来的,所以我现在最怀疑这个类引发我所有的问题,因为如上的javascript路径应该是正确的,可是一登陆就会出现异常页面,我也一起贴出来
<global-exceptions>
<exception key="general.exception" type="java.lang.Exception"
scope="request" handler="com.coreram.framework.exception.RedirectExceptionHandler"
path="/error.jsp" />
</global-exceptions>public class RedirectExceptionHandler
extends ExceptionHandler { private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager.getLogger("RedirectExceptionHandler");
public ActionForward execute(Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response) throws
ServletException {
ActionForward forward = null;
ActionError error = null;
String property = null;
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath(),true);
}
else {
forward = mapping.getInputForward();
} // Figure out the error
if (ex instanceof ModuleException) {
error = ( (ModuleException) ex).getError();
property = ( (ModuleException) ex).getProperty();
}
else {
logger.error(ex.getMessage());
error = new ActionError(ae.getKey(), ex.getMessage());
property = error.getKey();
} // Store the exception
request.setAttribute(com.coreram.framework.Globals.KEY_ERROR_MESSAGE, ex.getMessage());
storeException(request, property, error, forward, ae.getScope());
return forward;
}}
我之前做过一个小测试,如果直接调用一个jsp页面,action就可以传递过去,测试的小程序:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'Test.jsp' starting page</title>
</head> <body>
<form action="TestAction.do" method="get"> <input type="text" maxlength="12" name="userno"> <input type="submit" value="OK!">
</form>
</body>
</html>
action类package com.coreram.framework.common.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;import com.coreram.framework.manager.sysuser.SysUser;
import com.coreram.framework.util.Pub;public class TestAction extends DispatchAction { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { try {
String userno = Pub.val(request, "userno");
if (userno.equals("tom")) {
return mapping.findForward("success");
}
//SysUser user = (SysUser)request.getSession().getAttribute("cur_user");
//System.out.println(user.getUsername());
//System.out.println(user.getUserno());
//SysUser user = (SysUser) request.getSession().getAttribute(com.coreram.framework.Globals.USER_KEY);
} catch (Exception e) {
}
return mapping.findForward("failed");
}
}
求高人指点啊!!
<!--新增加的模块!功能是登陆进去根据登录用户的session取出用户的相应信息 -->
<action path="/newtask" scope="request" validate="false"
type="com.coreram.framework.common.action.NewTask" parameter="method" >
<forward name="ok" path="/success.jsp"/>
</action>
我自己的action类,只是为了测试,里面的代码基本可以无视:package com.coreram.framework.common.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.DispatchActionSupport;public class NewTask extends DispatchActionSupport{ public ActionForward newtask(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { if ("tom".equals("tom")) {
return mapping.findForward("ok");
}
return null;
}
}javascript:function doNewsTask(){
//task.jsp一开始加载首先调用这个函数,把action传过去,获取到当前session的username!!!
try { //alert("error");
//location.href ="/Test.jsp";这种写法是正确的!!
//window.location.href="/Test.jsp"; 这种写法是错误的!!
var url="/newtask.do";
window.location.href=url;
}
catch(e)
{alert("wrong");}
}
另外struts-congig.xml中的全局异常节点使用了一个异常处理类,因为程序不是我写的,我是新来的,所以我现在最怀疑这个类引发我所有的问题,因为如上的javascript路径应该是正确的,可是一登陆就会出现异常页面,我也一起贴出来
<global-exceptions>
<exception key="general.exception" type="java.lang.Exception"
scope="request" handler="com.coreram.framework.exception.RedirectExceptionHandler"
path="/error.jsp" />
</global-exceptions>public class RedirectExceptionHandler
extends ExceptionHandler { private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager.getLogger("RedirectExceptionHandler");
public ActionForward execute(Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response) throws
ServletException {
ActionForward forward = null;
ActionError error = null;
String property = null;
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath(),true);
}
else {
forward = mapping.getInputForward();
} // Figure out the error
if (ex instanceof ModuleException) {
error = ( (ModuleException) ex).getError();
property = ( (ModuleException) ex).getProperty();
}
else {
logger.error(ex.getMessage());
error = new ActionError(ae.getKey(), ex.getMessage());
property = error.getKey();
} // Store the exception
request.setAttribute(com.coreram.framework.Globals.KEY_ERROR_MESSAGE, ex.getMessage());
storeException(request, property, error, forward, ae.getScope());
return forward;
}}
我之前做过一个小测试,如果直接调用一个jsp页面,action就可以传递过去,测试的小程序:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'Test.jsp' starting page</title>
</head> <body>
<form action="TestAction.do" method="get"> <input type="text" maxlength="12" name="userno"> <input type="submit" value="OK!">
</form>
</body>
</html>
action类package com.coreram.framework.common.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;import com.coreram.framework.manager.sysuser.SysUser;
import com.coreram.framework.util.Pub;public class TestAction extends DispatchAction { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { try {
String userno = Pub.val(request, "userno");
if (userno.equals("tom")) {
return mapping.findForward("success");
}
//SysUser user = (SysUser)request.getSession().getAttribute("cur_user");
//System.out.println(user.getUsername());
//System.out.println(user.getUserno());
//SysUser user = (SysUser) request.getSession().getAttribute(com.coreram.framework.Globals.USER_KEY);
} catch (Exception e) {
}
return mapping.findForward("failed");
}
}
求高人指点啊!!
web.xml
<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>
和引入的 过滤器有关, 我的是public class TestAction extends ActionSupport
自己对比下
ActionSupport是struts2,我的是struts1, 呵呵,问题解决了,struts1的类继承错了,
public class NewTask extends DispatchActionSupport应该是BaseDispatchAction