Struts2 Web 应用程序 404 错误

Struts2 Web application 404 error

我正在使用

当我在 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> 

结构 我的项目结构是:

我猜那是因为您还没有告诉它要渲染什么。 web context根目录下没有内容,你没有在web.xml.

中指定欢迎文件

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

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

As from Struts 2.5 all filters were moved to dedicated package, see the example:

<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,那么您应该将 "default" 操作(空名称)映射到具有根 ("/") 命名空间的包。

例如

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

  <!-- root -->

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