2013-12-09 58 views
2

我想在struts 2上创建一个简单的登录表单,但是由于某种原因我在查看输入字段时遇到问题,并且在我提交用户名后没有出现...Struts 2登录表单问题

下面是代码

Struts的重定向到我struts.xml

struts.xml

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

<struts> 
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/> 
    <!-- devMode equals mode debug information and reload everything for every request --> 
    <constant name="struts.devMode" value="true" /> 

    <package name="user" namespace="/User" extends="struts-default"> 
     <action name="Login"> 
      <result>Login.jsp</result> 
     </action> 
     <action name="DashboardAction" class="action.DashboardAction"> 
      <result name="success">Dashboard.jsp</result> 
     </action> 
    </package> 




</struts> 

bean类

DashboardAction.java

package action; 

public class DashboardAction { 
    private String username; 

    public String execute(){ 
     return "success"; 
    } 

    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; 
} 



} 

JSP中

Login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Login</title> 
    </head> 
    <body> 
     <h1>Struts 2 Login Test</h1> 

      <form action="DashboardAction" id="form1" method="post" autocomplete="off"> 
       <div class="input placeholder"> 
        <s:textfield name="username" label="Utilizador"/> 
       </div> 
       <div class="input placeholder"> 
        <s:password name="password" label="Password"/> 
       </div> 
       <div class="submit"> 
        <s:submit value="Entrar" method="execute"/> 
       </div> 
      </form> 




    </body> 
</html> 

Dashboard.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Welcome User</title> 
    </head> 
    <body> 
     <h1>Hello 
     <s:property value="username"/> 
     </h1> 
    </body> 
</html> 

为什么我按下提交后不工作?它应该去Dashboard.jsp

回答

0

您应该将表格映射到接受提交的数据的操作。使用Struts标签

<%@ taglib prefix="s" uri="/struts-tags" %> 

<s:form action="Welcome" ... 
    <s:textfield name="username" ... 
    <s:password name="unknown" ... 

无法映射密码字段,因为在您的操作中没有属性。如果你创建它

private String unknown; 

public String getUnknown() { 
    return unknown; 
} 

public void setUnknown(String unknown) { 
    this.unknown = unknown; 
} 
+0

是没有帮助在所有...形式仍然没有出现或工作 – exceltior

+0

这是因为你的问题是不完整的。你输入什么网址? –

+0

其自动进入由http:// localhost:8084/struts2/ide的ide后,我按下提交它“post”到Dashboard.jsp问题是登录窗体甚至不会出现在页面中... – exceltior

1

这里可能是你走:

<result name="success">/Dashboard.jsp</result> 

您需要指定在搜索结果中的全路径的JSP。请注意0​​。您需要对您的其他结果页面执行相同操作,包括Login.jsp

0

在您的操作类中定义属性密码如下所示。

DashboardAction.java

package action; 

public class DashboardAction { 
    private String username; 
    private String password; // you don't have this line in your action class. 
    public String execute(){ 
     return "success"; 
    } 

    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; 
} 


}