刚接触struts 2,做了一个简单的登陆程序(一个action,3个jsp页面),运行时,struts 2有时会调用action的create方法,有时又调用index方法,有时又会调用execute方法。即便是在action配置中指定了method="execute"方法,仍然是一样的。????
说明:如果form使用struts 标签,每次都调用execute方法,method属性不起作用,但会提示the resource required is not found for action or result success错误.使用html 的form标签时,绝大多数时间是调用create方法,配置method参数不起作用,但运行正常。
非常迷惑,请大侠们解惑!
开发平台:MyEclipse 6.0.1,jdk 6,tomcat 6.0
源程序:
1,LoginAction.java
public class LoginAction extends ActionSupport{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private String user;
private String password;

public String create() throws Exception{
user = user+"--create_user";//加上“--create_user是为了测试",下同
password = password+"--create_password";
//user = new String(user.getBytes("ISO-8859-1"),"utf-8");
//String t = user;
return execute();
}

public synchronized String getUser() {
return user;
} public synchronized void setUser(String user) {
this.user = user;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}

public String index() throws Exception {
user=user+"--index_user";
password=password+"--index_password";
return execute();
}
public synchronized String getPassword() {
return password;
}
public synchronized void setPassword(String password) {
this.password = password;
}

2.loginframe.jsp
<body>
<center>
<!--  
<s:actionerror/>
<s:form action="userlog" method="post">
<s:textfield name="user" label="用户名"/>
<s:textfield name="password" label="密码"/>
<s:submit value="登陆"></s:submit>
</s:form>
--> <form action="userlog" method="post">
<table>
<tr>
<td> 用户名:</td><td><input type="text" name="user"/></td>
</tr>
<tr>
<td>密码:</td><td><input type="password" name="password"/>
</td>
<tr>
<td></td><td> <input type="submit" value="登陆"/></td>
</tr>
</table>
</form>
</center>

</body>
</html>
3.loginsuccess.jsp
<body>

<h1> 恭喜你终于登陆成功了!</h1>
您使用的名称是:<s:property value="user" default="none"/><br>
你输入的密码是:<s:property value="password" default="unkown"/>

</body>
</html>
4.loginerror.jsp
<body>
<h4 align="center">对不起,您还没有登陆,不能进行该项操作!</h4>

 <s:fielderror/>
</body>
</html>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="Thesis_Manager" version="2.4"
         xmlns="http://java.sun.com/xml/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">  <display-name>Thesis Manager</display-name>
  <!-- Filters -->
  <!-- START SNIPPET: filter -->
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <!-- END SNIPPET: filter -->    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Welcome file lists -->
    <welcome-file-list>
        <welcome-file>/login/loginframe.jsp</welcome-file>
    </welcome-file-list></web-app>6.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="thesis.actions" extends="struts-default">
     <action name="userlog" class="thesis.actions.LoginAction" method="create">
     <result name="success">/login/loginsuccess.jsp</result>
     <result name="input">/login/loginerror.jsp</result>
     </action>
    </package>
    <!-- Add packages here --></struts>没有properties文件

解决方案 »

  1.   

    没遇到过这种情况,
    默认情况下  如果不指定method 则调用execute()方法
    如果指定了,则调用指定的方法.
    <form action="userlog" method="post"> 改成
    <form action="userlog.action" method="post"> 试试呢?
      

  2.   

    如果改为< form action="userlog.action" method="post">则browser返回"the requested resource(/thesis/userlog.action)is not available."使用struts 标签和html标签效果相同。
      

  3.   

    补充:
        在这之前还出现过这样的现象:使用struts标签,browser返回:the required resource(/thesis/userlog.xhtml) is not available.也很奇怪?
      

  4.   

    感谢cust_28和sangshusen_1988,重新部署后,正常了。
      

  5.   

    不明白 为什么你每个方法都要去调用excute()方法