Java Struts2 Web应用程序404错误

Java Struts2 Web应用程序404错误,java,eclipse,struts2,http-status-code-404,Java,Eclipse,Struts2,Http Status Code 404,我正在使用 Eclipse 4.5.2 ApacheTomcat 7.0.34 Struts 2.5 当我在Eclipse中运行该项目时,我得到一个404错误 404错误: 我的项目中的代码如下所示: web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <

我正在使用

  • Eclipse 4.5.2
  • ApacheTomcat 7.0.34
  • Struts 2.5
当我在Eclipse中运行该项目时,我得到一个404错误

404错误:

我的项目中的代码如下所示:

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts 2 Web Application</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
<!-- 
   <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
 -->
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

<welcome-file-list>
  <welcome-file>welcome</welcome-file>
  <welcome-file>login</welcome-file>
</welcome-file-list>

</web-app>

Struts 2 Web应用程序
支柱2
org.apache.struts2.dispatcher.FilterDispatcher
支柱2
/*
欢迎
登录
login.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 localization example</h1>

<s:form action="validateUser" namespace="/user">

<s:textfield key="global.username" name="username" />
<s:password key="global.password" name="password"/> 
<s:submit key="global.submit" name="submit" />

</s:form>

<s:url var="localeEN" namespace="/" action="locale" >
   <s:param name="request_locale" >en</s:param>
</s:url>
<s:url var="localezhCN" namespace="/" action="locale" >
   <s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:url var="localeDE" namespace="/" action="locale" >
   <s:param name="request_locale" >de</s:param>
</s:url>
<s:url var="localeFR" namespace="/" action="locale" >
   <s:param name="request_locale" >fr</s:param>
</s:url>

<s:a href="%{localeEN}" >English</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
<s:a href="%{localeDE}" >German</s:a>
<s:a href="%{localeFR}" >France</s:a>

</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 localization example</h1>

<h4>
<s:property value="username"/>
</h4>

</body>
</html>

Struts2本地化示例
EN
中国
判定元件
fr
英语
中国人
德国的
法国
welcome.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 localization example</h1>

<s:form action="validateUser" namespace="/user">

<s:textfield key="global.username" name="username" />
<s:password key="global.password" name="password"/> 
<s:submit key="global.submit" name="submit" />

</s:form>

<s:url var="localeEN" namespace="/" action="locale" >
   <s:param name="request_locale" >en</s:param>
</s:url>
<s:url var="localezhCN" namespace="/" action="locale" >
   <s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:url var="localeDE" namespace="/" action="locale" >
   <s:param name="request_locale" >de</s:param>
</s:url>
<s:url var="localeFR" namespace="/" action="locale" >
   <s:param name="request_locale" >fr</s:param>
</s:url>

<s:a href="%{localeEN}" >English</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
<s:a href="%{localeDE}" >German</s:a>
<s:a href="%{localeFR}" >France</s:a>

</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 localization example</h1>

<h4>
<s:property value="username"/>
</h4>

</body>
</html>

Struts2本地化示例
struts.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts 2 Web Application</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
<!-- 
   <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
 -->
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

<welcome-file-list>
  <welcome-file>welcome</welcome-file>
  <welcome-file>login</welcome-file>
</welcome-file-list>

</web-app>



我猜这是因为您没有告诉它要渲染什么。web上下文的根目录中没有内容,并且您尚未在web.xml中指定欢迎文件


尝试向浏览器URL添加“/user/pages/welcome.jsp”(您还没有向我们展示的页面)并查看它是否呈现“welcome.jsp”页面可能会很有用。

在Struts 2.5中,
FilterDispatcher
不推荐使用。自从Struts 2.1.3以来,它就被弃用了,不应该在新版本中使用

从Struts 2.5开始,所有过滤器都被移动到专用包中,请参见 例如:


支柱2
org.apache.struts2.dispatcher.filter.strutspreadexecutefilter
...
resource not available
错误来自Tomcat,因为它找不到根文件夹的资源,例如欢迎文件
index.jsp

欢迎文件列表
可用于您的
web.xml


index.jsp

如果不想使用
欢迎文件列表
,则应将“默认”操作(名称为空)映射到具有根(
“/”
)命名空间的包

例如


/user/pages/welcome.jsp

什么是struts版本?对不起,我忘了提这个。我正在使用Struts 2.5。此时,我希望看到部署到Tomcat的WAR文件的实际内容。我不知道你会怎么想。@AliZakeri这个问题你解决了吗?我也遇到了同样的问题。StackOverFlow上的帖子对我没有帮助。谢谢你的回答,我对Eclipse不是很熟悉。您能解释一下如何将“/user/pages/welcome.jsp”添加到我的浏览器URL中吗?谢谢。您的问题与Eclipse无关,只是您显然正在使用Eclipse中的嵌入式浏览器测试您的应用程序。我只是说要更改浏览器中的URL,将“”更改为“”。如果它呈现了“welcome.jsp”页面的内容,则表明您的应用程序已正确部署,您只是没有正确配置欢迎文件。@DavidM.Karr Struts2用于浏览扩展名为“.action”或没有扩展名的操作,请不要直接访问jsp页面。感谢您的回答,我已经包含了“欢迎文件列表”但是,在我的web.xml文件(请参阅更新web.xml)中,问题仍然存在。您是否更新了
struts.xml
以在根包中包含空操作?如果在根命名空间中找不到操作配置,struts将无法为未与提供的请求url映射的操作提供服务。另外,请尝试从全新的安装包重新安装tomcat。我已经更新了struts.xml中的代码,但仍然收到错误。“请求的资源不可用”上下文/Struts2Example没有启动。您没有应用此答案的更新。