struts配置问题,求助啊
页面提交后老出现404问题,可能是很初级的问题了吧,找了很多老帖,各种修改各种无效,大家帮帮忙吧
下面是源码:
login.jsp
- Java code
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%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 'login.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> <form action="login.action" method="post""> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body></html>
web.xml
- XML code
<?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></web-app>
LoginAction.java
- Java code
package com.test.action;public class LoginAction { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception { return "success"; }}result.jsp
- Java code
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%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 'result.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> 用户名:${requestScope.username }<br> 密码:${requestScope.password }<br> </body></html>
struts.xml
- XML code
<?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="struts2" extends="struts-default"> <action name="login" class="com.test.action.LoginAction"> <result name="success">/result.jsp</result> </action> </package></struts>
login提交后的错误提示:
HTTP Status 404 - /struts/login.action
--------------------------------------------
type Status report
message /struts/login.action
description The requested resource (/struts/login.action) is not available.
--------------------------------------------
Apache Tomcat/7.0.26
[解决办法]
好像还有一个配置文件吧?配置后缀的
告诉机器是通过.do还是.action访问你的Action
[解决办法]
第一个jsp页面没有把struts2引进来 程序不知道你要用struts2;
- Java code
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%>//使用相应的标签<%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 'login.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> <form action="login.action" method="post""> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body></html>
[解决办法]
问题1:要继承struts2的ActionSupport
问题2: <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
[解决办法]
你只要把action方法中的类继承Action,或者ActionSupport,就可以了.
[解决办法]
- Java code
<s:form action="login" method="post""> <s:textfield name="username" label="username"></s:textfield> <s:password name="password" label="password"></s:password> <s:submit label="submit"></s:submit> </s:form>
------解决方案--------------------
action类继承ActionSupport类
[解决办法]
你的程序里面没有那些标签啊
[解决办法]
我不同意楼上说的,不用struts2的标签完全可以,用户的最终POST请求和用不用struts2标签有什么区别?用了struts2标签POST请求参数有什么不同吗?
楼主错在LoginAction没有继承ActionSupport类,因此系统不认为LoginAction类可以执行,所以报找不到login.action错误
[解决办法]
加个绝度i路径吧 。 <form action="<%=basePath%>login.action" method="post"">
[解决办法]
你的路径问题!
/struts/login.action
你的struts配置文件的问题。好好检查下。
[解决办法]
- Java code
public class LoginAction extends ActionSupport{
[解决办法]
- Java code
<action name="login" class="com.test.action.LoginAction">
[解决办法]
在页面为login.action,配置文件为login.do
1,改为统一的后缀
2,在struts.xml里配个常量为<constant name="struts.action.extension" value="action"/>
[解决办法]
- Java code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%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> <form action="login.action" method="post"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body></html>
[解决办法]
tomcat错误提示:
/struts/login.action
description The requested resource (/struts/login.action) is not available.
大概意思应该是没有找到请求描述资源(/struts/login.action)
楼主你仔细看看你的路径或者是包路径什么的 配置对不对
[解决办法]
初看没什么错误
建议楼主加上namespace=“/”配置项
在<package name="struts2" extends="struts-default">里面
[解决办法]
最好别用login,user之类的词命名action...
[解决办法]
404应该是路径问题吧,仔细看下路径问题。。。
[解决办法]
先说几个无关紧要的
1.<form action="login.action" method="post""> 你这个多了个引号
2.既然在web.xml中配置拦截方式是/* 那么login.action中的.action不加也可以的
建议action继承ActionSupport
- Java code
package com.test.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private String username; private String password; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { return SUCCESS; }}
[解决办法]
<form action="login.action" method="post"">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
把login.action的action 去掉试试···