1

这些是我正在使用的 jar 文件:

commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
struts2-dojo-plugin-2.3.8.jar
xwork-core-2.3.8.jar

struts.xml - 存在于项目的 web 内容文件夹中

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
 <struts>
    <package name="Login" extends="struts-default">
        <action name="login" class="org.jolly.struts.action.LoginAction">
                <result name="SUCCESS">register.jsp</result>
                <result name="LOGIN">login.jsp</result>
        </action>
    </package>

    <package name="Register" extends="struts-default">
        <action name="register" class="org.jolly.struts.action.FormAction">
                <result name="SUCCESS">success.jsp</result>

        </action>
    </package>

web.xml - 存在于 Web 内容文件夹中

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
 xmlns="http://java.sun.com/xml/ns/javaee"  
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RegisterForm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

动作类

登录操作.java

package org.jolly.action;

 import org.apache.commons.lang3.StringUtils;

 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction extends ActionSupport{

private String userId;
private String password;

public String execute(){
    if(getUserId().equals("user") && getPassword().equals("password"))
        return SUCCESS;
    else
        return LOGIN ;

}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getPassword() {
    return password;
}

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

public void validate(){

    if(StringUtils.isEmpty(getUserId())) {
        addFieldError("userId","UserId cannot be blank");
    }

    if(StringUtils.isEmpty(getPassword())) {
        addFieldError("password","Password cannot be blank");
    }
}

}

FormAction.java

package org.jolly.action;

  import org.apache.commons.lang3.StringUtils;
  import com.opensymphony.xwork2.ActionSupport;

  public class FormAction extends ActionSupport{

private String name;
private String emailId;

public String execute(){
    return SUCCESS;
}

public String getName() {
    return name;
}

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

public String getEmailId() {
    return emailId;
}

public void setEmailId(String emailId) {
    this.emailId = emailId;
}

   public void validate(){
    if(StringUtils.isEmpty(getName())) {
        addFieldError("name","Name cannot be blank");
    }

    if(StringUtils.isEmpty(getEmailId())) {
        addFieldError("emailid","Email Id cannot be blank");
    }
}

JSP 的

1.登录.jsp

  <%@ taglib prefix="s" uri="/struts-tags"%> 

<html>
<body>
<s:form action="login">
      <s:textfield label="User Id" key="userid"/>
      <s:password label="Password" key="password"/>
</s:form>
</body>
</html>

2.注册.jsp

    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib prefix="sx" uri="/struts-dojo-tags"%>

     <html>
     <head>
      <s:head/>
      <sx:head/>
     </head>
     <body>
     <s:form action="register">
     <s:textfield label="Enter Your Name" key="name"/>
     <s:textfield label="Enter Your Email Id" key="emailId"/>
     <s:checkbox name= "Gender" fieldvalue="Female" value="true"/>
     <s:checkbox name ="Gender" fieldvalue="Male"/>
     <s:checkbox name ="Gender" fieldvalue="Others"/>
     <sx:autocompleter label= "Country" list ="{'India','Pakistan','U.S'}"/>
     <s:textarea key="add" name="address"/>
     <sx:datetimepicker name="date" label="Date" displayFormat="dd/MM/yyyy"/>
    <s:submit value= "Submit Form"/>
 </s:form>
  </body>

3.成功.jsp

  <html>
  <body>
   Form is submitted successfully!!
 </body>
  </html>

错误

SEVERE: Dispatcher initialization failed
 Unable to load configuration. - action - 

 file:/C:/Users/JOLLY/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpweba 
 pps/RegisterForm%20(1)/WEB-INF/classes/struts.xml:7:69
    at  
 com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager. 
 java:70)
    at 
 org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:429)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:471)
    at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:193)
    at 
 org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:2
 81)
    at 
 org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:26
 2)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>
 (ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4656)
    at 
 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5312)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: Action class [org.jolly.struts.action.LoginAction] not found - action -  
 file:/C:/Users/JOLLY/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpweba
 pps/RegisterForm%20(1)/WEB-INF/classes/struts.xml:7:69
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigur
 ationProvider.java:480)
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurati
 onProvider.java:424)
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurat
 ionProvider.java:541)
    at c

 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigur
 ationProvider.java:290)
    at 
 org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurati
 onProvider.java:112)
    at 
 com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfigurat
 ion.java:239)
    at 
 com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.
 java:67)
    ... 16 more
    Feb 27, 2013 12:12:12 PM org.apache.catalina.core.StandardContext filterStart

 SEVERE: Exception starting filter struts2

 Unable to load configuration. - action - 
 file:/C:/Users/JOLLY/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpweba
 pps/RegisterForm%20(1)/WEB-INF/classes/struts.xml:7:69
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:483)
    at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:193)
    at 
 org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:2 81)
    at 
 org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:26 
 2)
    at org.apache.catalina.core.ApplicationFilterConfig.<init> 
 (ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4656)
    at 

 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5312)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: Unable to load configuration. - action - 
 file:/C:/Users/JOLLY/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpweba
 pps/RegisterForm%20(1)/WEB-INF/classes/struts.xml:7:69
    at 
 com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.
 java:70)
    at 
 org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:429)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:471)
    ... 14 more

 Caused by: Action class [org.jolly.struts.action.LoginAction] not found - action - 
 file:/C:/Users/JOLLY/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpweba
 pps/RegisterForm%20(1)/WEB-INF/classes/struts.xml:7:69
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigur
 ationProvider.java:480)
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurati
 onProvider.java:424)
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurat
 ionProvider.java:541)
    at 
 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigur
 ationProvider.java:290)
    at 
 org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurati
 onProvider.java:112)
    at 
 com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfigurat
 ion.java:239)
    at 
 com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.
 java:67)
    ... 16 more

     Feb 27, 2013 12:12:12 PM org.apache.catalina.core.StandardContext startInternal
     SEVERE: Error filterStart
     Feb 27, 2013 12:12:12 PM org.apache.catalina.core.StandardContext startInternal
     SEVERE: Context [/RegisterForm_(1)] startup failed due to previous errors
     Feb 27, 2013 12:12:12 PM org.apache.coyote.AbstractProtocol start
     INFO: Starting ProtocolHandler ["http-bio-8080"]
     Feb 27, 2013 12:12:12 PM org.apache.coyote.AbstractProtocol start
     INFO: Starting ProtocolHandler ["ajp-bio-8009"]
     Feb 27, 2013 12:12:13 PM org.apache.catalina.startup.Catalina start
     INFO: Server startup in 9975 ms

请任何人都可以提供帮助。两周以来我一直在尝试这个。

4

2 回答 2

0

错误消息准确地说明了问题所在。

您的配置文件为您的操作类使用一个包,您的操作类在另一个包中。

您的下一个问题将是找不到您的结果,因为配置文件使用所有大写的结果名称,而常量值都是小写的。

您还使用旧过滤器。为什么验证逻辑重复?

于 2013-02-27T12:24:11.057 回答
0

struts.xml 文件应该放在

任何一个

1) 在 Java 资源--> src 文件夹-->struts.xml 中。

或者

2)在WebContent-->WEB-INF-->classes(创建一个名为“classes”的新文件夹)--> struts.xml

于 2015-05-13T05:13:32.380 回答