2014-05-13 32 views
1

我是Struts的新手,我想执行HelloWorld程序,但是一旦我在web.xml中写入<filter>标记,我就会收到“请求的资源不可用”错误。Struts 2中没有的资源

Project hierarchy

web.xml

<?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_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

    <display-name>Struts 2</display-name> 
    <welcome-file-list> 
     <welcome-file>index.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> 
</web-app> 

struts.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
    <package name="helloworld" extends="struts-default"> 
     <action name="hello" class="sagar.practice.HelloWorldAction" method="execute"> 
      <result name="success">/HelloWorld.jsp</result> 
     </action> 
    </package> 
</struts> 

我添加了所有必需的支柱瓶当用户库。

我访问索引页为:http://localhost:8080/practice/index.jsp

如果我删除<filter>标签从web.xml文件我能够访问index.jsp页,但如果我添加<filter>标签这是给“请求的资源不可用”错误。

+0

index.jsp是在WEB-INF下?相同的HelloWorld.jsp? –

回答

1

您应该通过在struts.xml设定恒定导通devMode。如果您想要解决诸如您的问题,请使用此模式。它更具信息性,因为它输出到隐藏在正常模式下的控制台错误。

<constant name="struts.devMode" value="true" /> 

将它打开后,您可能会发现您的应用程序中部署了错误或不一致的库集。您可能会发现一个java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils打印到控制台,然后调整您的库集缺少罐子。

需要commons-lang3-3.1.jar来解决错误。

+0

我将index.jsp中的欢迎文件更改为web.xml中的default.jsp并尝试过,但它仍然给出相同的错误。并通过删除welcome-file-list标签进行检查,但没有结果。我通过将url-pattern修改为“/*.action”来检查它,但遇到同样的问题。 – Sagar

+0

你有没有清洁构建? –

+0

我从服务器中删除了项目并将其重新添加到服务器(也进行了清理) – Sagar

0

更改此内容在struts.xml中有以下

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 

参考链接:click here

如果您在使用撑杆2.0或更大的版本。使用下面的过滤器。

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
+0

更改并通过添加和删除过滤器标记进行检查,但它给出了相同的错误 – Sagar

+0

抱歉,这也没有帮助。做这些访问索引页面的效果吗?因为我没有调用任何操作类 – Sagar

0

执行以下操作变化的web.xml

<welcome-file>/WEB-INF/index.jsp</welcome-file> 

而在struts.xml中

<result name="success">/WEB-INF/HelloWorld.jsp</result> 

它可以帮助你。