struts2中action配置非execute方法method跳转失败,甚至进不了action

dasusu 2016-12-17 11:35:02

package com.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dao.PatientinfoDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.vo.PatientinfoDTO;

public class MyLoginAction02 extends ActionSupport {
private String Pname;
private String Ppassword;
private String psex;

public String getPsex() {
return psex;
}

public void setPsex(String psex) {
this.psex = psex;
}

static{ //用execute会执行,用method="changePsex"就不执行!!很烦恼 找不出问题
System.out.println("进来了!");
}
public String execute() throws Exception {
System.out.print(psex);//用execute会执行,用method="changePsex"就不执行!!很烦恼 找不出问题
PatientinfoDAO dao = new PatientinfoDAO();
String role = null;
PatientinfoDTO rs = new PatientinfoDTO();
rs = dao.findUserByAccountAndPassword(Pname, Ppassword);
if (rs != null) {
role = "patient";
Map session = ActionContext.getContext().getSession();
session.put("user", rs);
// System.out.print(patientLogin.get("name"));
session.put("role", role);
return "success01";
}

return "input";

}

public String changePsex() throws Exception {
System.out.print(psex);
PatientinfoDAO dao = new PatientinfoDAO();
int r = dao.updateSex(Pname, psex);
if (r > 0)
return SUCCESS;
else
return INPUT;
}

public String getPname() {
return Pname;
}

public void setPname(String pname) {
Pname = pname;
}

public String getPpassword() {
return Ppassword;
}

public void setPpassword(String ppassword) {
Ppassword = ppassword;
}

}
...全文
535 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm290903152 2016-12-29
  • 打赏
  • 举报
回复
struts2.5.8据说是为了更安全,进缺省方法index(),我在xml里指定了method,依旧不进我指定的方法
lm290903152 2016-12-29
  • 打赏
  • 举报
回复
struts.xml配置 ============================================================= <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="hjzx_xtyh" namespace="/" extends="struts-default" strict-method-invocation="false"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="index" class="testaction" method="index"> <result name="success">/index.jsp</result> </action> <action name="test_*" class="testaction" method="{1}"> <result name="success">/index.jsp</result> <allowed-methods>logingo,loginout</allowed-methods> </action> </package> </struts> ====================================================== jsp页面 ======================================================= <body> <a href="test_loginout.action?aaa=aaa"> test xtyhxxxxx</a> <form action="test_logingo.action"> 11111<input id="" name="aaa" value="222" /> <input type="submit" value="go" /> </form> <s:a action="test_logingo.action">redirect</s:a> </body> ========================================================== action ========================================================== package com.hope.hjzx.action; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.hope.hjzx.entity.test; import com.hope.hjzx.service.testService; import com.opensymphony.xwork2.ActionSupport; @Controller @Scope("prototype") public class testaction extends ActionSupport { private static final long serialVersionUID = 1L; @Resource private testService ts; private String methods; private String aaa; public String getMethods() { return methods; } public void setMethods(String methods) { this.methods = methods; } public String getAaa() { return aaa; } public void setAaa(String aaa) { this.aaa = aaa; } public String index() { System.out.println("哈哈哈哈哈哈" + aaa); return "success"; } public String loginout() { System.out.println("1111111===" + aaa); return "success"; } public String logingo() { System.out.println("222222==" + aaa); List list = ts.test(); test test = new test(); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { test = (test) list.get(i); System.out.println(test.getId()); } } return "success"; } } ==================================================================== jsp的三个方法,只能跳转到index()运行结果 =================================================================== 哈哈哈哈哈哈aaa 哈哈哈哈哈哈222 哈哈哈哈哈哈null
墨笙弘一 2016-12-29
  • 打赏
  • 举报
回复
return success 你写的return success01 你那个xml里面配的最后应该调用的是execute 但是成功了应该return success
lm290903152 2016-12-29
  • 打赏
  • 举报
回复
补充一句,我jar版本用的struts2-core-2.5.8.jar等,
lm290903152 2016-12-29
  • 打赏
  • 举报
回复
同问,楼主解决了吗?求struts.xml和action代码,在action继承com.opensymphony.xwork2.ActionSupport了吗?
dasusu 2016-12-17
  • 打赏
  • 举报
回复
引用 4 楼 m2200 的回复:
试了下,没有任何问题,正常进入changePsex方法,是不是你请求的时候用的是changePsex而不是changeOfPsex呢? 我请求路径是http://localhost:8080/struts2/changeOfPsex,后面是changeOfPsex
我是从jsp中的js进去的 function saveSex(){ var newSex = document.getElementsByName("newSex"); var url=null; for (var i = 0; i < newSex.length; i++) { if (newSex[i].checked) { //document.write(newSex[i].value); url = "changeOfPsex.action?psex=" + newSex[i].value; break; } } window.location.href = url; }
爱睡觉的阿狸 2016-12-17
  • 打赏
  • 举报
回复
试了下,没有任何问题,正常进入changePsex方法,是不是你请求的时候用的是changePsex而不是changeOfPsex呢? 我请求路径是http://localhost:8080/struts2/changeOfPsex,后面是changeOfPsex
dasusu 2016-12-17
  • 打赏
  • 举报
回复
引用 1 楼 m2200 的回复:
最关键的xml配置没有给出啊。
<action name="changeOfPsex" class="com.action.MyLoginAction02" method="changePsex"> <result name="success">usercentral_p.jsp</result> <result name="input">usercentral_p.jsp</result> </action>
dasusu 2016-12-17
  • 打赏
  • 举报
回复
<action name="changeOfPsex" class="com.action.MyLoginAction02" method="changePsex"> <result name="success">usercentral_p.jsp</result> <result name="input">usercentral_p.jsp</result> </action>
爱睡觉的阿狸 2016-12-17
  • 打赏
  • 举报
回复
最关键的xml配置没有给出啊。

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧