struts框架搭建及validator示例-阿里云开发者社区

开发者社区> 开发与运维> 正文

struts框架搭建及validator示例

简介: struts框架搭建 struts必备jar包 struts的校验(validation) struts的validation框架需要的jar包 struts的validation.xml:Local DTD is missing for publicID xwork-validator-1.0.2.dtd

包看就会,如果哪没看懂,欢迎评论区留言交流或者私信

@TOC

前置

配置tomcat

0 项目地址

地址
结构
在这里插入图片描述

1 创建web工程

    参考链接
    根据需要选择合适的tomcat
在这里插入图片描述

2 lib下导入jar包

strutsjakarta-oro
    其中jakarta-oro和commons-validator是因为validation框架导入的,其余是struts所必须的。(当然也可以去我的项目里面下载jar)
在这里插入图片描述

3 写代码

1 创建struts项目

(1)LoginAction.java

package validate;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private String a;
    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }
    @Override
    public String execute() throws Exception {
        System.out.println("action方法execute");
        if (getA().equals("admin")) {
            return SUCCESS;
        } else {
            super.addActionError("用户名错误!");
            return INPUT;
        }
    }
}

(2)struts的相关xml
    利用include标签,将一个struts.xml配置文件分割成多个配置文件,比较清晰

struts-validate.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- name package的名字    extends:继承的父package名称   -->
<!-- namespace:package命名空间。如目前login的是http://localhost:8080/login -->
    <package name="validate" extends="struts-default" namespace="/">
        <action name="login" class="validate.LoginAction" method="execute">
            <result name="success">/WEB-INF/pages/validate/success.jsp</result>
            <result name="input">/WEB-INF/pages/validate/index.jsp</result>
        </action>
    </package>
</struts>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <include file="/validate/struts-validate.xml"/>
</struts>

(3)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts-validation</display-name>
  <welcome-file-list>
    <welcome-file>/WEB-INF/pages/validate/index.jsp</welcome-file>
  </welcome-file-list>
    <!-- struts2 -->
    <filter>
        <filter-name>struts2</filter-name>
       <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
       </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

(4)jsp

  • validate="true"是开启验证,创建struts项目时可将此句删除
    index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="Java"
    import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        登录
        <s:form action="login" validate="true" method="post" namespace="/">
            <s:textfield name="a" id="a" label="a"></s:textfield>
            <s:textfield name="b" id="b" label="b"></s:textfield>
            <s:submit value="提交"></s:submit>
        </s:form>
        <s:actionerror />
    </body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="Java"
    import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        成功
    </body>
</html>

2 添加validate

1 相关知识
  • 1 validation.xml和*Action.java放在一起
  • 2 validation.xml命名规则
    (1)<ActionClassName>-validation.xml

    ActionClassName放的是要验证Action类的名字
(2)<ActionClassName>-<actionName>-validation.xml
    actionName指struts.xml里面配置的action name,如LoginAction-login-validation.xml

  • 3 s:form具备输出校验错误的能力
  • 4 前面struts.xml配置input,是为了让校验失败后,返回index.jsp
2 代码
  • 对b的校验采取的是自定义validator
    LoginAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" 
  "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
    <field name="a">
        <field-validator type="requiredstring">
             <message>此项必输</message>
        </field-validator>
        <field-validator type="stringlength">
             <param name="minLength">3</param>
            <message>长度必须大于3</message>
        </field-validator>
    </field>
    <field name="b">
        <field-validator type="bValidator">
            <param name="b">b</param>
            <message>bbbbb</message>
        </field-validator> 
    </field>
</validators> 

BValidator.java

package validate;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class BValidator extends FieldValidatorSupport {
    private String b;
    @Override
    public void validate(Object object) throws ValidationException {
        //获取字段名
        String fieldName = super.getFieldName();
        String value = (String)super.getFieldValue(fieldName,object);
        System.out.println("自定义validator"+fieldName+","+value);
        if(value == null || value == "") {
            //使LoginAction-validation.xml中b的message可以显示
            this.addFieldError(fieldName, object);
            //添加校验错误,和上一条语句任选其一即可
            this.getValidatorContext().addFieldError(fieldName, this.getValidatorContext().getText(value+"b为空"+b));
            return;
        }
    }
}

validators.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" 
  "http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd">
      <!-- 此DOCTYPE和default.xml中的一致 -->    
<validators>
<!-- 验证器注册 -->
  <validator name = "bValidator" class = "validate.BValidator"/>
</validators> 

4 启动中遇到的问题

(1)Caught Exception while registering Interceptor class org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor - interceptor - jar:file:/D:/eclipse/tools/apache-tomcat-7.0.86-windows-x64/apache-tomcat-7.0.86/wtpwebapps/struts-validation/WEB-INF/lib/struts2-core-2.3.37.jar!/struts-default.xml:222:127

解决:配置离线dtd(dtd资源),当然也可以在我的代码中找,就在src目录下面
配置方法:参考
(2)警告: No configuration found for the specified action: 'Login' in namespace
若是struts中配置了namespace="/",但s:form中没有配置,则会出现
(3)Local DTD is missing for publicID: -//OpenSymphony Group//XWork Validator 1.0.2
解决:更换LoginAction-validation.xml开头为

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

提升

struts2权威指南pdf完整版+源代码
链接 提取码:seks

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章