struts2框架数据验证改动

index.jsp 注册页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <s:form action="userAction" method="post">
      <s:textfield name="name" label="用户名"></s:textfield>
      <s:password name="password" label="密码" ></s:password>
      <s:submit value="注册"></s:submit>
      <s:reset value="重置"></s:reset>


    </s:form>
  </body>
</html>

 

show.jsp 成功跳转页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Title</title>
</head>
<body>
    <s:fielderror></s:fielderror>
    <s:property value="name"></s:property>
    <s:property value="password"></s:property>

</body>
</html>

struts.xml 配置页面

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <package name="action" extends="struts-default" strict-method-invocation="false">
        <action name="userAction" class="com.chenx.action.UserAction">
            <result name="input">index.jsp</result>
            <result name="success">show.jsp</result>
        </action>
    </package>


</struts>

 

UserAction类

package com.chenx.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
    private static final long serialVersionUID=1L;
    private String name;
    private String password;
    
    public String execute() throws Exception{
        return SUCCESS;
    }
    public void setName(String name) {
        this.name = name;
    }

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

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }


}

 

 

UserAction-validation.xml  验证配置  注意DOCTYPE的变化 因为改变了网址

<?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>
    <!-- 验证用户名 -->
    <field name="name">
        <field-validator type="requiredstring">
            <message>请输入用户名</message>
        </field-validator>
    </field>
    <!-- 验证密码 -->
    <field name="password">
        <field-validator type="requiredstring">
            <message>请输入密码</message>
        </field-validator>
    </field>
</validators>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值