struts.xml
<?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="struts" namespace="/" extends="struts-default">
<action name="login" class="com.test.LoginAction">
<result name="failure">/failure.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
===================
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="demo" version="2.4"
xmlns="http://java.sun.com/ml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
======================
LoginAction.java
package com.test;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport
{
    private String username;
    private String password;
    
    public String exucute() throws Exception {
        if(username.equals("struts"))
            return "success";
        else
            return "failure";
    }    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;
    }}==================
login.jsp
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="get" %>
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="login">
</form></body>
</html>不管在login.jsp页面中输入的用户名是什么,都只会跳转到success.jsp页面。在LoginAction.java中打断点,没有作用,没有经过LoginAction.java中的execute方法的处理。
问题出在哪儿了?
struts2

解决方案 »

  1.   

    重启eclipse、clear project都试过了。不管在login.jsp中输入的用户名是什么,都跑到success.jsp页面,在LoginAction.java中打断点,启动项目,访问login.jsp,提交表单,根本不会再execute方法的断点处停留。
      

  2.   

    还有,如果在webcontent下没有index.jsp,在项目上右键→run on server,然后直接访问localhost:8080/demo,console会报错。
      

  3.   

    现在关键是好像提交表单后根本没有被LoginAction.java处理,但是为什么又跳转到success.jsp页面呢?
      

  4.   

    楼主写代码不小心,execute方法名写错了,所以这个action里没有execute方法,自动返回success。。
      

  5.   

    把你的页面修改了下,你在JSP页面上增加Struts2的标签
    <s:form action="user_login.action" namespace="/" theme="simple">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    <input type="submit" value="确定">
    </s:form>
    改成这样的。struts.xml<package name="s" namespace="/" extends="struts-default">
    <action name="user_*" method="{1}" class="com.test.LoginAction">
    <result name="failure">/failure.jsp</result>
    <result name="success">/success.jsp</result>
    </action>
    </package>Action:
    public String login() throws Exception {
           System.out.println("xadsadsadsadsadsadasdasdsadddddddddddddddd");
    if(username.equals("struts"))
                return "success";
            else
                return "failure";
        }