Stuts2模型驱动和服务器端校验

1、Stuts模型驱动

1.1model模型类(Person)

package model;

public class Person {
    private String userName;
    private String password;

    @Override
    public String toString() {
        return "Person{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

1.2模型驱动action

package action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import model.Person;

public class PersonAction extends ActionSupport implements ModelDriven<Person> {
    //必须要实例化不然取不到数据
    private Person person=new Person();
    @Override
    public Person getModel() {
        return person;
    }
    public String form() {
        System.out.println(person);
        return SUCCESS;
    }
}

1.3配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="form" extends="struts-default">
        <action name="form" class="action.PersonAction" method="form">
            <result name="success">/success.jsp</result>
            <result name="input">/form.jsp</result>
        </action>
    </package>
</struts>

1.4jsp页面(正确编写jsp页面重中之重)

name必须和model实体类属性的名字一样。

<html>
<head>
    <title>表单服务器端校验</title>
</head>
<body>
<form action="form.action">
<table>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="userName"></td>
    </tr>
    <tr>
        <td>密码:</td>
        <td><input type="password" name="password"></td>
    </tr>
    <tr>
        <td><input type="submit" value="提交"></td>
    </tr>
</table>
</form>
</body>
</html>

2、Struts做服务器端校验(声明式校验)

2.1在上面基础上修改jsp页面

          name必须和model实体类的属性名一致,
          并且必须把表单给action,让action做检查和匹配(模型驱动)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>表单服务器端校验</title>
</head>
<body>
<form action="form.action">
<table>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="userName"></td>
        <td> <s:fielderror fieldName="userName"></s:fielderror></td>
    </tr>
    <tr>
        <td>密码:</td>
        <td><input type="password" name="password"></td>
        <td> <s:fielderror fieldName="password"></s:fielderror></td>
    </tr>
    <tr>
        <td><input type="submit" value="提交"></td>
    </tr>
</table>
</form>
</body>
</html>

2.2在action包下建立PersonAction-validation.xml文件(名字必须这么写)

        xml文件命名规范:action名字-validation.xml
         <!--fieldName必须和表单name一致-->
    <param name="fieldName">userName</param>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <validator type="requiredstring">
        <!--fieldName必须和表单name一致-->
        <param name="fieldName">userName</param>
        <message>姓名不能为空</message>
    </validator>
    <validator type="regex">
        <param name="fieldName">password</param>
        <param name="regex">\w{6,8}</param>
        <message>请输入6-8位字母、数字或者下划线</message>
    </validator>
</validators>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值