404错误,第一个struts2程序.
我从网上完完整整抄过来的程序,但是无法运行成功,弄了好几天了。请大家帮忙看看哪里出现了问题。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错误,问题出现在哪里呢?
struts2 --------------------编程问答-------------------- web.xml
配置
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
--------------------编程问答-------------------- 你那个是*
url访问:http://localhost:8080/struts/test/hello
这样就可以了 --------------------编程问答--------------------
改了,还是404错误。 --------------------编程问答-------------------- 是在你原来是基础上改的吧?重启 tomact了没、 --------------------编程问答--------------------
也不行。 --------------------编程问答--------------------
什么都没有改,照搬过来的。重启了。也重新发布了。也不行。 --------------------编程问答-------------------- 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 --------------------编程问答--------------------
你项目名称是struts嘛? --------------------编程问答--------------------
是呀
--------------------编程问答-------------------- <action name="hello" method="execute">
改成
<action name="hello" class="com.club.web.action.HelloAction" method="execute">
好像不只这一个错误,你先把这个改了试下,这代码哪来的? --------------------编程问答-------------------- http://localhost:8080/struts/
这样默认就是访问 index.jsp页面啊 不可能访问不了啊 --------------------编程问答--------------------
改了,也不行,来源:http://singandsuny.blog.163.com/blog/static/1866688220101195258138/ --------------------编程问答--------------------
下次看之前先看下评论,评论好的才看,这个好多错误
<result name="success">/WEB-INF/page/hello.jsp</result>
改成<result name="success">page/hello.jsp</result> 在试下 --------------------编程问答-------------------- 你在第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>
做完这些应该可以获取到了吧。 --------------------编程问答-------------------- action是要交给对应的action类来处理的,通过<action name="***" class="***">来配置 --------------------编程问答-------------------- <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了 --------------------编程问答-------------------- JSP页面中把第一句改为<%@page contentType="text/html; charset=UTF-8"%> --------------------编程问答-------------------- 理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法。 --------------------编程问答-------------------- 好多错误 顶 --------------------编程问答-------------------- 把jsp文件不要放到web-inf下
把page文件下的hello.jsp放到webroot下,完后再修改一下struts.xml试试
--------------------编程问答--------------------
<result name="success">/page/hello.jsp</result>
<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
补充:Java , Java EE