struts2没有调用action类自定义的方法,跳转404

一直没有调用action方法,控制台无报错信息,登陆跳转404。
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">
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>login.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>

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <constant name="struts.action.extension" value="true" />
    <package name="strut2" extends="struts-default">
        <action name="Login" class="loginAction.LoginAction" method="user">
            <result name="usersuccess">/user.jsp</result>
            <result name="adminsuccess">/admin.jsp</result>
            <result name="fail">/fail.jsp</result>
        </action>
    </package>
</struts>

试过将method改为函数名称,也试过删去
aciton类:

package loginAction;

public class LoginAction {
    private String account;
    public String getAccount(){
        return account;
    }

    public void setAccount(String account){
        this.account=account;
    }

    private String password;
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String user()throws Exception{
        System.out.print('1');
        if(account.equals(password)){
            System.out.println("执行user方法");
            return "usersuccess";
        }
        return "fail";
    }

    public String admin()throws Exception{
        if(account.equals(password)){
            System.out.println("执行admin方法");
            return "adminsuccess";
        }
        return "fail";
    }

}

试过删去set,get方法不行,函数没有调用(控制台无1输出)
login.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    普通用户登录:
    <form action = "login-user" method="post">
      请您输入账号:<input name="account" type="text"><br>
      请您输入密码:<input name="password" type="password">
      <input type="submit" value="登录">
    </form><br>

    管理员用户登录:
    <form action = "login-admin" method="post">
      请您输入账号:<input name="account" type="text"><br>
      请您输入密码:<input name="password" type="password">
      <input type="submit" value="登录">
    </form>
  </body>
</html>

试过将action 改为/形式
其他三个登陆成功(失败)页面相似:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        普通用户登陆成功。
    </body>
</html>

确认struts2包齐全,由myeclipse内部struts2.2.1,struts.xml也在tomcat中的class文件夹中生成

2个回答

你的Action类名头部要定义@Action哦,例如:
@Action(value = "HospCataAction", results = {
@Result(name = "cataManage", location = "/pages/biz/medicare/catalog/xx.jsp")}
public xx xxx{
public String login(){
.....
}
}
然后你的jsp中要改成xxx/HospCataAction!login.action

拦截后缀是针对 URL 请求的,需要在 from 的 action 中添加后缀,改成这样试试:

  <form action = "login-user.action" method="post">
      请您输入账号:<input name="account" type="text"><br>
      请您输入密码:<input name="password" type="password">
      <input type="submit" value="登录">
    </form><br>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问