Struts2 同一个Action中处理增删改查四种方法, Struts.xml该怎么配?
我的Action中含有增删改查四种方法、现在struts.xml是这样配的、 现在比如说JSP页面上的from表单要执行Action中的删除方法 、该怎么写? 比如说现在Action中的删除执行完了要立即执行全查、xml中 <result name="success" type="redirectAction">student.dosearch</result> 这样配正确吗?
<struts>
<package name="struts2" namespace="" extends="struts-default">
<action name="login" class="com.sy.Action.LoginAction" method="doLogin">
<result name="success" type="redirectAction">student.dosearch</result>
<result name="error">/login.jsp</result>
</action>
<action name="student" class="com.sy.Action.StudentAction"
method="doSearch">
<result name="success">/index.jsp</result>
</action>
<action name="student" class="com.sy.Action.StudentAction"
method="deDel">
<result name="success" type="redirectAction">dosearch</result>
</action>
<action name="student" class="com.sy.Action.StudentAction" method="toupdate">
<result name="success">/Update.jsp</result>
</action>
</package>
</struts>
[解决办法]
这是我的一个struts+spring的例子供参考
<?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="struts.multipart.saveDir" value="/tmp"/>
<include file="struts-default.xml" />
<package name="json" extends="json-default" namespace="/employee">
<action name="findAllEmployee" class="employeeAction" method="queryAllEmployee">
<result type="json">
<param name="root">pageResult</param><!-- 注意:这里不是可有可无的,否则得话页面无法获得数据。这个是用来告诉ExtJs分页信息要从action的哪一个变量中获取,好像变量要设Page这里不配置也行 -->
</result>
</action>
<action name="updateEmployee" class="employeeAction" method="modifyEmployeeInfo">
<result type="json">
<param name="root">pageResult</param>
</result>
</action>
<action name="findeSpecEmployee" class="employeeAction" method="querySpecialEmployee">
<result type="json">
<param name="root">object</param>
</result>
</action>
</package>
</struts>
[解决办法]
配置ACTION时的name 不一样
后面的不变就行了
[解决办法]
然后要全查你直接写对应的action配置的那么名就好了呀
[解决办法]
action的名称唯一。另外,你也可以通过使用通配符的方式实现。
[解决办法]
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.daoyuan.base.action.BaseAction;
import com.daoyuan.common.bean.HrExam;
import com.daoyuan.common.bean.HrPostClass;
import com.daoyuan.common.bean.HrPostTrans;
import com.daoyuan.common.tool.PageTool;
import com.daoyuan.hr.service.intf.HrPostClassService;
@Controller("hrPostClassAction")
@Scope("prototype")
@SuppressWarnings("unused")
public class HrPostClassAction extends BaseAction {
@Resource(name = "hrPostClassServiceImpl")
private HrPostClassService hrPostClassService;
private HrPostClass hrPostClass = new HrPostClass();
public HrPostClass getHrPostClass() {
return hrPostClass;
}
public void setHrPostClass(HrPostClass hrPostClass) {
this.hrPostClass = hrPostClass;
}
/**
* 查询岗位类别列表
*
* @return
*/
public String doQuery() {
if(request.getParameter("winOpen")!=null){
request.setAttribute("winOpen",request.getParameter("winOpen"));
}
try {
LinkedHashMap<String, String> orderby = PageTool.setOrder(
"postClassId", "asc");
ArrayList<String> propertyName = new ArrayList<String>();
propertyName.add("postClassId");
propertyName.add("postClassName");
propertyName.add("sort");
propertyName.add("isDelete");
propertyName.add("organId");
ArrayList<String> eql = new ArrayList<String>();
eql.add("=");
eql.add("like");
eql.add("=");
eql.add("=");
eql.add("=");
PageTool.getResult(request, HrPostClass.class, hrPostClassService,
PageTool.SYSTEMEND, propertyName, eql, new Object[] {
hrPostClass.getPostClassId(),
hrPostClass.getPostClassName(),
hrPostClass.getSort(), hrPostClass.getIsDelete()
,this.getCurrentUser().getOrganId()},
new String[] { "and", "and", "and", "and","and" }, orderby);
return GOLIST;
} catch (Exception ex) {
ex.printStackTrace();
wrtExceptionLog(ex);
return null;
}
}
/**
* 绑定数据到添加页面
*
* @return
*/
public String goAdd() {
this.hrPostClass.setPostClassId(hrPostClassService
.getKeyId("Hr_PostClass"));
return "goAdd";
}
/**
* 添加岗位类别
*
* @return
*/
public String doAdd() {
try {
if(getCurrentUser()!=null){
getHrPostClass().setOrganId(this.getCurrentUser().getOrganId());
this.hrPostClassService.save(this.hrPostClass);
wrtOperateLog("Hr_PostClass", "增加一条id为"
+ this.hrPostClass.getPostClassId() + "的数据");
response.sendRedirect("hrPostClass_doQuery.do");
return null;
}
return ERROR;
} catch (Exception ex) {
wrtExceptionLog(ex);
return this.ERROR;
}
}
/**
* 绑定岗位类别单个信息
*
* @return
*/
public String goInfo() {
return null;
}
/**
* 删除岗位类别
*
* @return
*/
public String doDelete() {
try {
this.hrPostClassService.delete(HrPostClass.class,request.getParameter("postClassId").toString());
wrtOperateLog("Hr_PostClass", "删除了一条id为"
+ request.getParameter("postClassId").toString() + "的数据");
response.sendRedirect("hrPostClass_doQuery.do");
return null;
} catch (Exception ex) {
wrtExceptionLog(ex);
return this.ERROR;
}
}
/**
* 岗位类别编辑
*
* @return
*/
public String doUpdate() {
try {
if(getCurrentUser()!=null){
getHrPostClass().setOrganId(this.getCurrentUser().getOrganId());
this.hrPostClassService.update(this.hrPostClass);
wrtOperateLog("Hr_PostClass", "修改了一条id为"
+ this.hrPostClass.getPostClassId() + "的数据");
response.sendRedirect("hrPostClass_doQuery.do");
return null;
}
return ERROR;
} catch (Exception ex) {
wrtExceptionLog(ex);
return this.ERROR;
}
}
/**
* 绑定数据跳转到修改页面
*
* @return
*/
public String toUpdate() {
this.hrPostClass=this.hrPostClassService.find(HrPostClass.class,request.getParameter("postClassId").toString());
return "goEdit";
}
}
<action name="hrPostClass_*" class="hrPostClassAction" method="{1}">
<result name="goAdd">/page/hr/Hr_PostClass/hrPostClassAdd.jsp</result>
<result name="goList">/page/hr/Hr_PostClass/hrPostClassQuery.jsp
</result>
<result name="goEdit">/page/hr/Hr_PostClass/hrPostClassEdit.jsp
</result>
</action>
在页面上的时候就是hrPostClass_toUpdate.do
hrPostClass_doDelete.do
[解决办法]
Action 代码如下:
import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class CRUDAction extends ActionSupport { // 参数及参数的get、set方法省略 /** * 添加 * @return */ public String add(){ return "add"; } /** * 删除 * @return */ public String del(){ return "del"; } /** * 修改 * @return */ public String update(){ return "update"; } /** * 查询 * @return */ public String find(){ return "find"; }}