论坛首页 Java企业应用论坛

关于struts2中@validations对于方法的校验

浏览 10976 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-11  
在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方法。真的很奇怪。不知道是哪里配置的问题。
   发表时间:2007-05-11  
抱歉,create方法上注解用红色字体表示没有显示出来。谢谢各位。
0 请登录后投票
   发表时间:2007-05-11  
我的action很长,你可以不看,其实就是为了说明方便。
0 请登录后投票
   发表时间:2007-05-12  
我觉得struts2采用annotation的方式配置没有采用配置文件集中配置好,配置文件的可维护行更好一些
感觉struts2也是在盲目的追求新技术
0 请登录后投票
   发表时间:2007-05-14  
这个是我后来找到的一个资料,大家看看就明白了。xly_971223   同志说的对,用配置文件可能好一些。
http://www.nabble.com/struts2-validation-for-only-one-method-in-action-tf3267302.html#a9083597
0 请登录后投票
   发表时间:2007-05-15  
貌似你的class头上少了一个@Validation
http://struts.apache.org/2.0.6/docs/validation-annotation.html
0 请登录后投票
   发表时间:2007-05-15  
写少了个括号,嘻嘻
@Validation()
加在public class RoleAction...头上
0 请登录后投票
   发表时间:2007-05-16  
我最初有的,后来去了,不管用。它应该只是表明进行类级别的检验。
0 请登录后投票
   发表时间:2007-05-24  
除了public String input()
其他任何名字的方法都会validate
真拿它没办法
我就是不想写xml
0 请登录后投票
   发表时间:2007-05-24  
看来input()应该是Override了ActionSupport的方法,所以没有被validate

+看这帖

然后这个input()好像说是在struts-default.xml里面定义了skip validation,可以在配置package的时候将'validation
interceptor configured differently
'(不过我们不写xml)

+看这帖

最后终于得到了一把金钥匙
@SkipValidation
+看这帖

为什么就这么简单一个注释,搞了那么久才google出来,请上帝原谅我的SB。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics