
struts2 404问题,高手帮忙看看,是哪里路径写错了,现在可以执行到action,在return 的时候报的404。急
//actionpackagecom.action;importcom.opensymphony.xwork2.ActionSupport;publicclassUser...
//action
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String login() throws Exception {
System.out.println("来了");
return "success";
}
}
<!--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="user" extends="struts-default">
<action name="login" class="com.action.UserAction">
<result name="success" >index.jsp</result>
</action>
</package>
</struts>
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>MyJsp.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>*.action</url-pattern>
</filter-mapping></web-app>
MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>
<body>
<form action="user!login.action" mentod="post">
<table>
<tr>
<td>姓名:</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
<input type="submit" name="button" value="确定">
<input type="reset" name="button" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
index.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>My JSP 'index.jsp' starting page</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>
This is my JSP page. <br>
</body>
</html>
工程如下图 展开
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String login() throws Exception {
System.out.println("来了");
return "success";
}
}
<!--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="user" extends="struts-default">
<action name="login" class="com.action.UserAction">
<result name="success" >index.jsp</result>
</action>
</package>
</struts>
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>MyJsp.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>*.action</url-pattern>
</filter-mapping></web-app>
MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>
<body>
<form action="user!login.action" mentod="post">
<table>
<tr>
<td>姓名:</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
<input type="submit" name="button" value="确定">
<input type="reset" name="button" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
index.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>My JSP 'index.jsp' starting page</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>
This is my JSP page. <br>
</body>
</html>
工程如下图 展开
5个回答
展开全部
你说已经进入到了action中了,也就是说在console中会显示“来了”。404错误说明路径有问题,找不到相应的视图;你的代码写错了,你在form表单中写带是action="user!login",应该是action="login!login",你怎么写成包名了呢?
展开全部
把Package name 变成空试试,如果不想改成空,就在<result name="success" >index.jsp</result>加上user变成<result name="success" >/user/index.jsp</result>
不知道要不要加上第一个“/”,试下吧。
不知道要不要加上第一个“/”,试下吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<result name="success" >index.jsp</result>
改为<result name="success" >/index.jsp</result>
表示工程webroot目录下的index.jsp
改为<result name="success" >/index.jsp</result>
表示工程webroot目录下的index.jsp
追问
/index.jsp
还是不好使
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<result name="success" >/index.jsp</result>要加/的
追问
/index.jsp 这样还是报错,请问还会有其他原因吗?
追答
404的错误就是路径不对啊,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
struts.xml的action
在action加上 method=“login”
在action加上 method=“login”
更多追问追答
追问
提交表单的时候有到 UserAction里面去,只是跳转页面的时候报的404,请问还有其他原因吗?谢谢。
追答
看见有啥不一样的了吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询