struts2表单校验问题
先看下我的配置
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="do,," />
<constant name="struts.custom.i18n.resources" value="global-message"/>
<package name="category" extends="struts-default">
<action name="category_*" class="categoryAction" method="{1}">
<result name="success">/WEB-INF/page/welcome.jsp</result>
<result name="input">/WEB-INF/page/category/save-ui.jsp</result>
<result name="saveUi">/WEB-INF/page/category/save-ui.jsp</result>
</action>
</package>
</struts>
类:CategoryAction
package bookstore.web.struts.action.category;
import org.apache.struts2.interceptor.validation.SkipValidation;
import bookstore.domain.Category;
import bookstore.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class CategoryAction extends ActionSupport implements
ModelDriven<Category> {
private static final long serialVersionUID = 2732491100881541453L;
private CategoryService categoryService;
private Category category = new Category();
@SkipValidation
public String saveUi() {
return "saveUi";
}
public String save() {
return SUCCESS;
}
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public Category getModel() {
return category;
}
}
校验框架,CategoryAction-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="required">
<message>名称 是必须输入的!</message>
</field-validator>
</field>
</validators>
页面:save-ui.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/page/head.jspf" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加分类</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body style="text-align:center">
<hr>
<h2>添加分类</h2>
<s:form action="/category_save">
<s:textfield name="name" label="name" />
<s:textfield name="description" label="description" />
<s:submit>
</s:form>
</body>
</html>
问题是:action中的方法saveUi() 不需要校验,于是我在方法上添加了注解@SkipValidation
加了之后发现不仅仅是方法saveUi() 不校验,连方法save()都不给我校验了,
要是把注解@SkipValidation 去掉的话那么 可以校验save()但是两个方法都会被校验,
这是为什么呢?
该如何解决呢?也是说只让框架struts2框架校验我的sava方法.不校验saveUi方法,?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你试试把
CategoryAction-validation.xml改成
CategoryAction-category_save-validation.xml试试看,我一般都没用
@SkipValidation这种方式,而是用ActionClassName-alias-validation.xml这种方式。还有一种方式,见
http://icemoon.iteye.com/blog/95620
你的jsp里没有加这个语句吧<%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld"%>
试试,我也学习下!呵呵