一个简单关于struts2的login问题。
JAVA代码:
package cn.cjh.action;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;public class LoginAction implements Action {

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;
} @Override
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Integer counter = (Integer)ctx.getApplication().get("counter");
if(counter == null)
{
counter = 1;
}
else
{
counter += 1;
}
ctx.getApplication().put("counter", counter);
ctx.getSession().put("user", getUsername());
if(getUsername().equals("cjh")&&getPassword().equals("cjh"))
{
ctx.put("tip", "登录成功");
return SUCCESS;
}
else
{
ctx.put("tip", "不成功");
return ERROR;
}

}}
struts.xml文件位置放在SRC下。
<?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="cn.cjh.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.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">

 <filter>
        <filter-name>struts2</filter-name>
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    </filter>    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping></web-app>
login.jsp 代码:
<%@ page contentType="text/html; charset=GBK" language="java"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密&nbsp;&nbsp;码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
</tr>
</table>
</form>
</body>
</html>
报的错误:
HTTP Status 404 - /cjh/struts2/WebRoot/login.action--------------------------------------------------------------------------------type Status reportmessage /cjh/struts2/WebRoot/login.actiondescription The requested resource (/cjh/struts2/WebRoot/login.action) is not available.
代码和书上的一样。但是一直报这个错误。网上找了许多答案 依旧报错。求各位大虾帮助啊!!!!

解决方案 »

  1.   

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

  2.   

    404报错的意思就是资源访问不到、在html表单上试着加这个
    <form action="/login.action" method="post">在struts.xml试着加
    <package name="struts2" extends="struts-default" namespace="/">再试试看、
      

  3.   

    HTTP Status 404 - /cjh/struts2/WebRoot/login.action404错误找不到那个/cjh/struts2/WebRoot/login.action,你看看你的action,是不是有错
      

  4.   

    action路径 有问题 你看看  HTTP Status 404 - /cjh/struts2/WebRoot/login.action
      

  5.   

    The requested resource (/cjh/struts2/WebRoot/login.action) is not available. 
    找不到action
      

  6.   

    发布的时候WebRoot一般不会带上,只需要工程名
      

  7.   

    应该是部署的不对 你在tomcat\webApps下建一个目录:cjh然后把你的webroot中的所有文件 文件夹放到 cjh目录中
    启动tomcat: http://localhost:8080/cjh/login.action
    或者 修改 tomcat\conf\server.xml在 </Host>前加入 
    <Context debug="0" docBase="盘符/cjh/struts2/WebRoot" path="/a" reloadable="true" >
    </Context>

          </Host>启动tomcat: http://localhost:8080/a/login.action
      

  8.   

    今天我把 TOMCAT的 server.xml 中的 <Context> 节点删除之后。 直接用工程名访问的 JSP 就找到了 action. 其他的和原来的一样。 如果有和我一样问题的同学可以试试啊 。 再次谢谢各位的帮助。!