2
回答
struts2表单校验问题
华为云4核8G,高性能云服务器,免费试用   

先看下我的配置

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方法,?

 

举报
gezexu
发帖于8年前 2回/746阅
顶部