【问题标题】:Struts2 Web application 404 errorStruts2 Web 应用程序 404 错误
【发布时间】:2016-10-11 17:25:22
【问题描述】:

我正在使用

  • Eclipse 4.5.2
  • Apache Tomcat 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>

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>

welcome.jsp

<%@ 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>

struts.xml

<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />

<package name="user" namespace="/user" extends="struts-default">
    <action name="login">
        <result>pages/login.jsp</result>
    </action>
    <action name="validateUser" class="com.mkyong.user.action.LoginAction">
        <result name="SUCCESS">pages/welcome.jsp</result>
        <result name="input">pages/login.jsp</result>
    </action>
</package>

<package name="default" namespace="/" extends="struts-default">
    <action name="locale" class="com.mkyong.common.action.LocaleAction">
        <result name="SUCCESS">user/pages/login.jsp</result>
    </action>
</package>


<package name="emptyaction" namespace="/" extends="default">
<default-action-ref name="" />
<action name="">
    <result type="redirect">/</result>
</action>
</package> 

结构 我的项目结构是:

【问题讨论】:

  • 什么是struts版本?
  • 对不起,我忘了提。我正在使用 Struts 2.5。
  • 此时,我想查看部署到 Tomcat 的 WAR 文件的实际内容。我不确定你会如何找到它。
  • @AliZakeri 这个问题你解决了吗?我遇到了同样的问题。StackOverFlow 上的帖子对我没有帮助。

标签: java eclipse struts2 http-status-code-404


【解决方案1】:

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

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

【讨论】:

  • 谢谢你的回答,我对Eclipse不是很熟悉。您能否解释一下如何将“/user/pages/welcome.jsp”添加到我的浏览器 URL 中?谢谢。
  • 您的问题与 Eclipse 无关,只是您显然是在使用 Eclipse 中的嵌入式浏览器测试您的应用程序。我只是说要更改该浏览器中的 URL,将“localhost:8080/Struts2Example”更改为“localhost:8080/Struts2Example/user/pages/welcome.jsp”。如果它呈现“welcome.jsp”页面的内容,则说明您的应用已正确部署,只是您没有正确配置欢迎文件。
  • @DavidM.Karr Struts2 用于浏览动作,有或没有“.action”扩展名的,不要直接访问jsp页面。
【解决方案2】:

在 Struts 2.5 中,FilterDispatcher 已被弃用。它自 Struts 2.1.3 起已被弃用,不应在较新的版本中使用。

从 Struts 2.5 开始,所有过滤器都移至专用包,请参阅 例子:

<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

resource not available 错误来自 Tomcat,因为它无法找到根文件夹的资源,例如欢迎文件 index.jsp

welcome-file-list 可用于您的web.xml

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

如果您不想使用welcome-file-list,那么您应该将“默认”操作(名称为空)映射到具有根("/")命名空间的包。

例如

<package name="root" namespace="/" extends="struts-default">

  <!-- root -->

  <action name="">
    <result>/user/pages/welcome.jsp</result>
  </action>

【讨论】:

  • 感谢您的回答,我已在 web.xml 文件中包含“welcome-file-list”(请参阅​​更新 web.xml),但是问题仍然存在。
  • 您是否更新了struts.xml 以在根包中包含空操作?如果没有在根命名空间中找到操作配置,struts 将无法为您的操作提供服务,该操作未与提供的请求 url 映射。也尝试从全新安装包中重新安装 tomcat。
  • 我已经更新了我的 struts.xml 中的代码,但仍然出现错误。
  • "请求的资源不可用"
  • 上下文 /Struts2Example 没有启动。您没有应用此答案的更新。
猜你喜欢
  • 1970-01-01
  • 2015-12-10
  • 2014-10-15
  • 2015-07-12
  • 1970-01-01
  • 2022-10-01
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多