gpt4 book ai didi

java - 使用 struts2 预填充表单元素并进行验证错误

转载 作者:行者123 更新时间:2023-12-01 13:56:40 25 4
gpt4 key购买 nike

我有一个带有几个文本框的表单。使用 customerSer 操作将文本框值初始化为默认值。

我可以更改默认值并使用 customer 操作提交表单。这两个操作都属于同一个类,每个操作使用不同的方法。

只要我没有对表单进行任何验证,这就可以正常工作。应用表单验证后,第一个操作将不起作用并给出错误并查找结果类型input

我需要对表单元素进行 struts2 验证。是否可以采取其他方法来形成数据群体或验证?

Struts.xml

<action name="customerSer"
class="net.test.struts2.action.TestAction" method="initialize">
<result name="none">Customer.jsp</result>
</action>

<action name="customer"
class="net.test.struts2.action.TestAction">
<result name="success">SuccessCustomer.jsp</result>
<result name="input">Customer.jsp</result>
</action>

TestAction.java

package net.viralpatel.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 7154564763591606533L;
private String name;
private Integer age;
private String email;
private String telephone;

public String execute() {
return SUCCESS;
}

public String initialize() {
System.out.println("Aditya");
this.name="Aditya";
this.age=28;
return NONE;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

TestAction-Validation.xml

<!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">
<param name="trim">true</param>
<message key="errors.required" />
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message key="errors.required" />
</field-validator>
<field-validator type="int">
<param name="min">1</param>
<param name="max">100</param>
<message key="errors.range"/>
</field-validator>
</field>
<field name="email">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
<field-validator type="email">
<message key="errors.invalid" />
</field-validator>
</field>
<field name="telephone">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
</field>
</validators>

Customer.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Customer Form - Struts2 Demo | ViralPatel.net</title>
</head>

<body>
<h2>Customer Form</h2>

<s:form action="customer.html" method="post" validate="true">
<s:textfield name="name" key="name" size="20" />
<s:textfield name="age" key="age" size="20" />
<s:textfield name="email" key="email" size="20" />
<s:textfield name="telephone" key="telephone" size="20" />
<s:submit key="label.add.customer" align="center" />
</s:form>
</body>
</html>

预填充数据 customer.jsp 的链接

<body>
<h2>Howdy, <s:property value="username" />...!</h2>
<s:a href="customerSer.html">Add Customer</s:a>
</body>

最佳答案

当您的验证 xml 文件命名为 TestAction-validation.xml 时,这意味着 TestAction 类中的所有操作都会发生验证过程。因此,您的验证是按操作类配置的,为了按照 struts.xml 文件中给出的操作名称配置它,您需要将验证文件命名为 TestAction-customer-验证.xml.

关于java - 使用 struts2 预填充表单元素并进行验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com