Struts2的项目访问时出现404错误的问题。

fendoudewoniu 2012-04-07 12:29:36
本人新学Struts2,建立了第一个简单的项目Struts2_0100_Introduction,

struts.xml文件为:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<include file="example.xml"/>



<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->

<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/Hello.jsp
</result>
</action>
</package>
<!-- Add packages here -->

</struts>

Hello.jsp文件为:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>HelloStruts2</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
Hello Struts2 <br>
</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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

但访问时出现如下错误:
HTTP Status 404 - /Struts2_0100_Introduction/hello.action

--------------------------------------------------------------------------------

type Status report

message /Struts2_0100_Introduction/hello.action

description The requested resource (/Struts2_0100_Introduction/hello.action) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.35

路径敲的是http://localhost:8080/Struts2_0100_Introduction/hello.action,请问错误可能在哪里?
...全文
1266 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011124989 2014-03-11
  • 打赏
  • 举报
回复
完美回答: 首先自己建立一个webproject,比如叫Qing ,把struts.xml内容加上 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <action name="hello"> <result> /sueecss.jsp </result> </action> </package> <!-- Add packages here --> </struts> 然后 把web.xml内容添加上,复制楼主的就行。 再然后在webroot目录下建立一个succes.jsp 。把struts2 所有的jar包加入lib目录下就行了。
Misheral 2013-03-01
  • 打赏
  • 举报
回复
楼主怎么样解决的?
wl759299833 2012-10-11
  • 打赏
  • 举报
回复
我也遇到这问题了,还没解决啊
dxqrr 2012-04-16
  • 打赏
  • 举报
回复
配置有问题
zhang352954605 2012-04-16
  • 打赏
  • 举报
回复
404一般是路径问题 一步步的看一下action
yjflinchong 2012-04-16
  • 打赏
  • 举报
回复
配置错了。
walkman_22 2012-04-15
  • 打赏
  • 举报
回复
你用myeclipse帮你布署试试。
有一只柴犬 2012-04-07
  • 打赏
  • 举报
回复
你的hello.jsp 是放在那个目录下的。。。
znnevergiveup 2012-04-07
  • 打赏
  • 举报
回复
路径问题,把jsp直接放到 webroot 目录下试试
陌路花开 2012-04-07
  • 打赏
  • 举报
回复
大神给力~~~~
  • 打赏
  • 举报
回复
尚学堂。。。。。struts2.1.6


路径问题跟路径有关系 404错误是资源访问地址不存在。

主要看你工程的目录结构。
Struts2_0100_Introduction
--src
|____ struts.xml


--WebRoot(or WebContent)
|
|___Hello.jsp



而你这种配置我说实话真不清楚 action 如果没用class 到底走不走result
后来自己建立个项目配置了一个没有class的action 确实走了。
那就证明楼主基本配置都没用错

我的配置:
struts.xml(就配置一个)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="test" namespace="/" extends="struts-default">
<action name="hello">
<result>/index.jsp</result>
</action>
</package>
</struts>



index.jsp(还是自动生成的)在WebRoot(or WebContent)下

打 http://localhost:8080/项目名/hello.action

我这样配置会正确的显示出index.jsp的内容


然后又仔细看了看楼主的情况、
感觉可能是在package name="default" 这里。

试着换个包名。只要不重复就可以。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧