为什么我的web.xml一配置structs运行就报错404,新手初学,就高人指点啊!!

mold_mind 2016-12-25 11:46:39
小弟初学框架。但我的web.xml一配置structs运行就报错404
下载的struct版本是2.5.8

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ServletProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configutation 2.3//EN"
"http://atruts.apache.org/dtds.struts-2.3.dtd">

<structs>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default">
<!-- 定义action -->
<action name="first">
<result>/first.jsp</result>
</action>

</package>
</structs>


在网上查了好多方法,但还是一直报错,头都要炸了= =。。就高手指点一下,万分感谢
...全文
201 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello-java-maker 2016-12-25
  • 打赏
  • 举报
回复
404错误有很多原因: 1.首先看后台有没有报错。有的话,看看什么错误,这样很快就找到了。 2.如果没有报错,如果你的页面是放在WEB-INFO下的话,需要通过action才能访问到,否则就会404. 3.你所访问的页面路径是否正确
zhuolinqing111 2016-12-25
  • 打赏
  • 举报
回复
哦 还发现一点 你的Action没有加class吧 要找到对应的文件 格式:<action name="doLogin" class="cn.jbit.auction.Action.LoginAction">
zhuolinqing111 2016-12-25
  • 打赏
  • 举报
回复
你把XML文件最上面的<web>改一下看看: <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">
mold_mind 2016-12-25
  • 打赏
  • 举报
回复
还有问题就是点了链接 不会跳转到jsp页面去

感觉是路径问题。但是看不出来哪里错了


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


<struts>
<!-- 声明包 -->
<package name="user" extends="struts-default" namespace="/user" >
<!-- 定义action -->
<action name="userAction" class="com.wgh.action.UserAction" >
<result name="add">/user_add.jsp</result>
<result name="update">/user_update.jsp</result>
</action>
</package>
</struts>



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ServletProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>



<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="user/userAction!add">add</a>
<a href="user/userAction!update">update</a>
</body>
</html>
爱睡觉的阿狸 2016-12-25
  • 打赏
  • 举报
回复
action标签里没有class属性配置,
Scaarf 2016-12-25
  • 打赏
  • 举报
回复
不报错才怪。 <package>标签中还有namespace(这个好像可写可不写,不清楚反正我一直都写) 最重要的<action>标签中的class属性和method属性你都没指定, class对应的引号中填的是你action类(注意是包名加类名) method对应的引号中填的是action类中的方法名,不写method属性的话,默认execute(这种情况写,就得保证你的action类里有execute方法) 另外,<result> 标签有name属性,对应的引号里填上面method对应引号里填的方法的返回字符串,不写name属性的话默认success(同样,不写name属性的话,得保证上所面指定方法的返回字符串是success) 给个例子吧, action类
public class TestActionContextAction {
	public String testActionContext(){
		return "success";
	}

}
配置文件

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

<struts>
	<package name="struts2" extends="struts-default" namespace="/">
		
		<action name="actionContext" class="com.abc.struts2.action.TestActionContextAction" method="testActionContext">
			<result name="success">/WEB-INF/pages/main.jsp</result>
		</action>
		
	</package>
</struts>
话说,为什么有听老师讲课么。。。。。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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