我使用带有struts 2 xml字段验证(tomcat7)的netbeans 8来创建一个Web应用程序。形成。当我单击提交按钮而不填写表单时,将显示成功页面,没有错误将表单留空。我错过了将LoginAction-validation.xml
连接到表单的步骤吗?也许我需要更新插件?我在Libraries文件夹中有以下内容用于表单验证:
Struts2 Core 2.3.15 - xwork-core-2.3.15.3.jar
Struts2 Core 2.3.15 - struts2-core-23.15.3.jar
LoginAction.java
package login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperationSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
public String getVersion() {
return version;
}
public void setVersion(String pass) {
this.version = version;
}
String operatingSystem;
String version;
String notes;
@Override
public String execute(){
return "SUCCESS";
}
}
的index.jsp
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jive</title>
</head>
<body>
<h1>Code Sample</h1>
<s:form action="LoginAction" method="post">
<s:textfield label="Operating System" name="operatingSystem"/>
<s:textfield label="Version" name="version"/>
<s:textfield label="Notes" name="notes"/>
<s:submit cssClass="btn btn-primary"/>
</s:form>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml中
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="LoginAction" class="login.LoginAction" method="execute">
<result name="SUCCESS">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
的LoginAction-validation.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 2.3//EN"
"http://struts.apache.org/dtds/xwork-validator-2.3dtd">
<validators>
<field name="operatingSystem">
<field-validator type="requiredstring">
<message key="errors.required">Operation System is required.</message>
</field-validator>
</field>
<field name="version">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
</field>
</validators>
的success.jsp
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1> Success </h1>
</body>
</html>
error.jsp文件
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Error!</h1>
</body>
</html>
答案 0 :(得分:0)
正如您在the Apache DTD directory中看到的那样,没有像xwork-validator-2.3dtd
这样的东西(扩展中也有拼写错误)。最新的xwork-validator是1.0.3,它是你应该使用的那个:
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
同时提醒始终使用相同版本的库,例如2.3.15.3
(或更好2.3.16.1
)无处不在