gpt4 book ai didi

java - Struts2 Web应用404错误

转载 作者:行者123 更新时间:2023-11-30 08:36:28 25 4
gpt4 key购买 nike

我正在使用

  • 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>

登录.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>

结构我的项目结构是:

enter image description here

最佳答案

在 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>
...

资源不可用 错误来自 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>

关于java - Struts2 Web应用404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37753395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com