首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

关于struts2中@validations对于步骤的校验

2012-11-12 
关于struts2中@validations对于方法的校验在struts2中,我使用下面的方法请求。通过Action!Method.action的

关于struts2中@validations对于方法的校验
在struts2中,我使用下面的方法请求。
通过Action!Method.action的方法实现了dispatchAction。现在的情况是action里的execute已经没有了。被若干个crud方法取代。

现在想使用@validations用来针对crud方法中的变量做校验.但是比如下面的action.

package com.tongcard.merchant.web.actions.privilege;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.struts2.config.Namespace;import org.apache.struts2.config.Result;import org.apache.struts2.config.Results;import org.apache.struts2.dispatcher.ServletRedirectResult;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;import com.opensymphony.xwork2.validator.annotations.Validation;import com.opensymphony.xwork2.validator.annotations.Validations;import com.opensymphony.xwork2.validator.annotations.ValidatorType;import com.tongcard.merchant.web.helper.BaseAction;import com.tongcard.merchant.web.helper.BusinessResult;import com.tongcard.merchant.web.helper.PageHelperList;import com.tongcard.merchant.web.manager.PrivilegeManager;import com.tongcard.merchant.web.manager.impl.PrivilegeManagerImpl;import com.tongcard.merchant.web.model.Role;import com.tongcard.merchant.web.model.User;@Namespace("/privilege")@Results( {@Result(name = "success", value = "/privilege/roleSearch.jsp"),@Result(name = "input", value = "/privilege/roleNew.jsp", type = ServletRedirectResult.class),@Result(name = "modify", value = "/privilege/roleModify.jsp"),@Result(name = "login", value = "/login/login.jsp") })public class RoleAction extends BaseAction {/** *  */private static final long serialVersionUID = 1L;private static int pageSize = 5;private int currentPage = 1;private PageHelperList pageHelper;private Role role;private List<String> pages;private String actions;private String info;private String roleId;private List<Role> roles;private PrivilegeManager privilegeManager;[color=red]@Validations(requiredStrings = { @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "role.roleName", message = "角色名称不能为空") })[/color]public String create() {BusinessResult bres = null;role.setOrgCode(request.getSession().getAttribute("orgCode").toString());bres = privilegeManager.newRole(role);info = bres.getInfo();return Action.INPUT;}// 分页查询得到当前管理员所在机构下的角色。public List<Role> buildRoleDivPageData(HashMap<String, String> map) {List<Role> roles = null;int count = privilegeManager.countRole(map.get("orgCode"));if (count == 0) {return null;}pageHelper = new PageHelperList(count, currentPage, pageSize);map.put("begin", pageHelper.getPageStartRow() + "");map.put("end", pageHelper.getPageEndRow() + "");roles = privilegeManager.searchRoleByPage(map);pages = new ArrayList<String>();for (int i = 0; i < pageHelper.getTotalPages(); i++) {pages.add(String.valueOf(i + 1));}return roles;}public String search() {HashMap<String, String> map = new HashMap<String, String>();User user = (User) (request.getSession().getAttribute("user"));if (null == user) {return "login";}map.put("orgCode", user.getOrgCode());roles = buildRoleDivPageData(map);return Action.SUCCESS;}public String show() {role = privilegeManager.searchSimpleRoleByRoleId(Long.valueOf(roleId));return "modify";}public String init() {return Action.INPUT;}// @Validations(requiredStrings = { @RequiredStringValidator(type// =ValidatorType.SIMPLE, fieldName = "role.roleName", message = "角色名称不能为空")// })public String update() {BusinessResult bres = null;bres = privilegeManager.modifyRole(role);info = bres.getInfo();request.setAttribute("role", role);return "modify";}// 得到全部当前管理员所在机构下的角色public List<Role> getRoles(String orgCode) {List<Role> list = null;list = privilegeManager.searchRolesByOrg(orgCode);return list;}public String getActions() {return actions;}public void setActions(String actions) {this.actions = actions;}public PrivilegeManager getPrivilegeManager() {return privilegeManager;}public void setPrivilegeManager(PrivilegeManagerImpl privilegeManager) {this.privilegeManager = privilegeManager;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}public void setPrivilegeManager(PrivilegeManager privilegeManager) {this.privilegeManager = privilegeManager;}public Role getRole() {return role;}public void setRole(Role role) {this.role = role;}public List<String> getPages() {return pages;}public void setPages(List<String> pages) {this.pages = pages;}public static int getPageSize() {return pageSize;}public static void setPageSize(int pageSize) {RoleAction.pageSize = pageSize;}public int getCurrentPage() {return currentPage;}public void setCurrentPage(int currentPage) {this.currentPage = currentPage;}public PageHelperList getPageHelper() {return pageHelper;}public void setPageHelper(PageHelperList pageHelper) {this.pageHelper = pageHelper;}public String getRoleId() {return roleId;}public void setRoleId(String roleId) {this.roleId = roleId;}public List<Role> getRoles() {return roles;}public void setRoles(List<Role> roles) {this.roles = roles;}}

create方法上的红色字体表示create时的一个校验。但是当我执行另一个请求:role!search.action时,却会执行role!create.action方法。真的很奇怪。不知道是哪里配置的问题。 <interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack"/>
</package>

然后
在struts 配置文件里面加入:
        <action name="save" method="save">
        <interceptor-ref name="defaultStack"/>
        <result name="input">../user/editOrganization.jsp </result>
<result name="success" type="redirect">list.action</result>
        </action>

热点排行