从 JSP 发出请求时出现 404 错误
我正在尝试学习 Struts2,但无法创建可用的 hello world 应用程序。当从 JSP 发出请求时,会出现 404(页面未找到)错误。我将所有 lib 文件保存在 lib 文件夹中,并将 struts.xml
放入类文件夹中,但我仍然收到此错误。
这是视图:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<form action="HelloWorld">
<input type="submit">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这是我的 struts.xml
文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="vaannila.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
这是错误:
HTTP 状态 404 - /Testi/HelloWorld
输入状态报告
消息/Testi/HelloWorld
描述请求的资源(/Testi/HelloWorld)不可用。
Apache Tomcat/6.0.33
I am trying to learn Struts2, but I am unable to create a working hello world application. It's giving a 404 (page not found) error when request is made from JSP. I kept all the lib files in the lib folder, placed struts.xml
inside classes folder, but I am still getting this error.
Here is the view:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<form action="HelloWorld">
<input type="submit">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is my struts.xml
file
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="vaannila.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
This is the error:
HTTP Status 404 - /Testi/HelloWorld
type Status report
message /Testi/HelloWorld
description The requested resource (/Testi/HelloWorld) is not available.
Apache Tomcat/6.0.33
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这与您在Struts2:使用表单操作中发布的问题相同,只是更详细一些。
您的问题出在您正在使用的表单标签中。它不是一个 struts form 标签,并且 struts form 标签中的 action 属性在 HTML 标签中的 action 属性中的处理方式不同。
我认为我在Struts2:使用表单操作中的答案应该适合您。
PS:如果您没有更改操作扩展,则只需在标准 HTML 标记操作的末尾添加 .action 即可实现此操作:
This is the same question that you posted in Struts2 : Using form action just a bit more verbose.
You're problem is in the form tag you are using. It's not a struts form tag and the action attribute in a struts form tag is handled differently in a action attribute in an HTML tag.
I think my answer in Struts2 : Using form action should work for you.
PS: if you haven't changed your action extension, you may be able to make this work by simply adding a .action onto the end of your standard HTML tag action:
我建议在 tomcat.webapps 文件夹下创建一个新文件夹,并根据标准放置所有文件,如果您使用任何 IDE,请尝试一下。
尝试从头开始调试,
你能够通过。
正确的。
我认为这应该是一个映射问题。
I would suggest to create a new folder under tomcat.webapps folder and place all your files according to the standards and give a try if you are using any IDE.
Try debugging from scratch,
you are able to go through.
correct.
I think this should be a mapping issue.
可以贴一下错误日志内容吗?以及关于项目的结构。404指示您的服务器能够找到所请求的资源,这可能是您的映射问题,或者可能无法在类路径中找到映射文件。
您需要将 struts.xml 文件放在类路径中,因此如果您使用 Eclipse,只需将 struts.xml 文件放入 src 文件夹中,其余 Eclipse 知道如何处理这个问题
另外,学习 struts2 的最佳方法是从那里下载示例应用程序官方网站
示例应用程序:
这将帮助您理解和更好地学习 struts2,因为
Can you post the error log content? and about the structure of the project.404indicatng the your server to able to locate the requested resources this can be a issue of your mapping or may be t unable to find the mapping file in class path.
you need to place struts.xml file in the class path so if you are using Eclipse just drop your struts.xml file inside src folder rest eclipse know how to handle this
Also the best way to learn struts2 is to download the sample applications from there official site
Example Applications:
this will help you to understand and learn struts2 better because
将导入的 lib 添加到 Action 类
ex:
Add imported lib to Action class
ex: