404错误,第一个struts2程序.

zyr08 2013-10-23 07:42:11
我从网上完完整整抄过来的程序,但是无法运行成功,弄了好几天了。请大家帮忙看看哪里出现了问题。
struts2 教程之第一个HelloWord示例
1.把struts2开发至少所需要的六个jar包导入到lib下。
2.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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

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

3.新建一个HelloAction类
package com.club.web.action;

public class HelloAction {
private String message = "Hello,Baby !";

public String getMessage() {
return message;
}

public String execute() {
return "success";
}
}

4.在src下新建一个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>
<package name="default" namespace="/test" extends="struts-default">
<action name="hello" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>

5.在/WEB-INF/page下新建一个hello.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>第一个struts2.1.8示例</title>
</head>

<body>
${message}@-@
</body>
</html>

6.发布该程序,url访问:http://localhost:8080/struts/test/hello.action
-------------------------------------------------------------
访问提示404错误,问题出现在哪里呢?
...全文
360 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyr08 2014-01-13
  • 打赏
  • 举报
回复
原来是myeclipse里设置的jdk版本与实际版本不一致照成的。唉,到现在才知道。感谢各位热心人。
ddplayer12 2013-10-24
  • 打赏
  • 举报
回复
把jsp文件不要放到web-inf下 把page文件下的hello.jsp放到webroot下,完后再修改一下struts.xml试试

 <result name="success">/page/hello.jsp</result>
u010016988 2013-10-24
  • 打赏
  • 举报
回复
好多错误 顶
-阿克蒙德- 2013-10-24
  • 打赏
  • 举报
回复
理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法。
shpery 2013-10-24
  • 打赏
  • 举报
回复
JSP页面中把第一句改为<%@page contentType="text/html; charset=UTF-8"%>
shpery 2013-10-24
  • 打赏
  • 举报
回复
<struts> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="com.club.web.action.HelloAction"> <result name="success">/page/hello.jsp</result> </action> </package> </struts> actio里最好指明包,同时是默认执行execute()方法 result访问里不需要WEB-INF目录 你在地址栏输入http://localhost:8080/struts/hello.action就ok了
老伯⭐️ 2013-10-24
  • 打赏
  • 举报
回复
引用 12 楼 a470577391 的回复:
<action name="hello" method="execute"> 改成 <action name="hello" class="com.club.web.action.HelloAction" method="execute"> 好像不只这一个错误,你先把这个改了试下,这代码哪来的?
大哥,怎么能在WEB-INF下存放页面呢, WEB-INF是Java的WEB应用的安全目录。所谓安全就是客户端无法访问,只有服务端可以访问的目录。 如果想在页面中直接访问其中的文件,必须通过web.xml文件对要访问的文件进行相应映射才能访问。 WEB-INF文件夹下除了web.xml外,还存一个classes文件夹,用以放置 *.class文件,这些 *.class文件是网站设计人员编写的类库,实现了jsp页面前台美工与后台服务的分离,使得网站的维护非常方便。web.xml文件为网站部署描述XML文件,对网站的部署非常重要。 在WebRoot下建立/pages/hello.jsp 再试试 http://localhost:8080/struts/test/hello
苏美尔叹息 2013-10-23
  • 打赏
  • 举报
回复
action是要交给对应的action类来处理的,通过<action name="***" class="***">来配置
晓晓魔导师 2013-10-23
  • 打赏
  • 举报
回复
你在第4步的时候错了, <struts> <package name="default" namespace="/test" extends="struts-default"> <!-- 下面的action缺少了个class属性 是用来指定哪个Action类的 所以如下 --> 错误:<action name="hello" method="execute"> 真确:<action name="hello" class="com.club.web.action.HelloAction" method="execute"> <!-- result标签下面指定的地址是这样的 --> 错误:<result name="success">/WEB-INF/page/hello.jsp</result> 真确:<result name="success">page/hello.jsp</result> </action> </package> </struts> 做完这些应该可以获取到了吧。
白开水MD5 2013-10-23
  • 打赏
  • 举报
回复
引用 14 楼 zyr08 的回复:
[quote=引用 12 楼 a470577391 的回复:] <action name="hello" method="execute"> 改成 <action name="hello" class="com.club.web.action.HelloAction" method="execute"> 好像不只这一个错误,你先把这个改了试下,这代码哪来的?
改了,也不行,来源:http://singandsuny.blog.163.com/blog/static/1866688220101195258138/[/quote] 下次看之前先看下评论,评论好的才看,这个好多错误 <result name="success">/WEB-INF/page/hello.jsp</result> 改成<result name="success">page/hello.jsp</result> 在试下
zyr08 2013-10-23
  • 打赏
  • 举报
回复
引用 12 楼 a470577391 的回复:
<action name="hello" method="execute"> 改成 <action name="hello" class="com.club.web.action.HelloAction" method="execute"> 好像不只这一个错误,你先把这个改了试下,这代码哪来的?
改了,也不行,来源:http://singandsuny.blog.163.com/blog/static/1866688220101195258138/
a465606179 2013-10-23
  • 打赏
  • 举报
回复
http://localhost:8080/struts/ 这样默认就是访问 index.jsp页面啊 不可能访问不了啊
白开水MD5 2013-10-23
  • 打赏
  • 举报
回复
<action name="hello" method="execute"> 改成 <action name="hello" class="com.club.web.action.HelloAction" method="execute"> 好像不只这一个错误,你先把这个改了试下,这代码哪来的?
zyr08 2013-10-23
  • 打赏
  • 举报
回复
引用 10 楼 a465606179 的回复:
你项目名称是struts嘛?


是呀
a465606179 2013-10-23
  • 打赏
  • 举报
回复
你项目名称是struts嘛?
zyr08 2013-10-23
  • 打赏
  • 举报
回复
引用 7 楼 a465606179 的回复:
http://localhost:8080/struts
访问不了。 我将web.xml中的 <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> 删了的话,可以访问到index.jsp的内容,但也无法访问hello.jsp
a465606179 2013-10-23
  • 打赏
  • 举报
回复
访问上面那个路径看有木有问题
a465606179 2013-10-23
  • 打赏
  • 举报
回复
http://localhost:8080/struts
zyr08 2013-10-23
  • 打赏
  • 举报
回复
引用 4 楼 a465606179 的回复:
是在你原来是基础上改的吧?重启 tomact了没、
什么都没有改,照搬过来的。重启了。也重新发布了。也不行。
zyr08 2013-10-23
  • 打赏
  • 举报
回复
引用 2 楼 a465606179 的回复:
你那个是* url访问:http://localhost:8080/struts/test/hello 这样就可以了
也不行。
加载更多回复(4)

67,515

社区成员

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

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