gpt4 book ai didi

java - Struts2 xml 验证不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:58:01 24 4
gpt4 key购买 nike

这是我的 Register-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>

<validator type="requiredstring">
<param name="fieldname">userBean.username</param>
<message>User name is required.</message>
</validator>

<validator type="required">
<param name="fieldname">profileBean.Age</param>
<message>Age is required.</message>
</validator>

</validators>

这是我的表格。

<s:form action="register">

<label>UserName</label>
<s:textfield cssClass="formfield" name="userBean.username"
label="User Name" />
<br />
<label>Password</label>
<s:password cssClass="formfield" name="userBean.password"
label="Password" />
<br />
<label>First Name</label>
<s:textfield cssClass="formfield" name="profileBean.firstName"
label="First Name" />
<br />
<label>Last Name</label>
<s:textfield cssClass="formfield" name="profileBean.lastName"
label="Last Name" />
<br />
<label>Age</label>
<s:textfield cssClass="formfield" name="profileBean.age" label="Age" />
<s:submit />
</s:form>

即使我没有输入用户名和年龄的值,该表单仍会继续显示在成功页面中。为什么它不起作用?

编辑

struts.xml
<?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="false" />
<constant name="struts.ui.theme" value="simple" />

<package name="basicstruts2" namespace="/Welcome" extends="struts-default">
<action name="index">
<result>WEB-INF/index.jsp</result>
</action>

<action name="HelloWorld" class="actions.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>

<package name="Registration" namespace="/" extends="struts-default">
<action name="register" class="actions.Register"
method="execute">
<result name="success">/thankyou.jsp</result>
</action>
</package>
</struts>

配置文件 Bean

package model;

public class Profile {
private String firstName;
private String lastName;
private int age;

public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}



}

和用户Bean

package model;

public class User {
private String username;
private String password;
private int accountType;//Admin - 0; User - 1;
private Profile profile;

public User(){
accountType = 1;
}


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;
}
public int getAccountType() {
return accountType;
}
public void setAccountType(int accountType) {
this.accountType = accountType;
}
public Profile getProfile() {
return profile;
}

public void setProfile(Profile profile) {
this.profile = profile;
}


}

注册操作

public class Register extends ActionSupport  implements ServletRequestAware{

public String execute(){

userBean.setProfile(getProfileBean());
return "success";
}

private User userBean;
private Profile profileBean;
HttpServletRequest request;

public User getUserBean() {
return userBean;
}

public void setUserBean(User userBean) {
this.userBean = userBean;
}

public Profile getProfileBean() {
return profileBean;
}

public void setProfileBean(Profile profileBean) {
this.profileBean = profileBean;
}

public void setServletRequest(HttpServletRequest request) {
this.request = request;
}

public HttpServletRequest getServletRequest() {
return this.request;
}
}

最佳答案

我试过运行你的代码。您的错误是您需要提供名为 input 的结果。如果验证失败,将查找此结果。

<package name="Registration" namespace="/" extends="struts-default">
<action name="register" class="actions.Register"
method="execute">
<result name ="input">erropage.jsp</result>
<result name="success">/thankyou.jsp</result>
</action>
</package>

关于java - Struts2 xml 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13916385/

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