2016-03-08 40 views
0

我是struts的新手用戶。這是我用struts.I不明白如何在JSP頁面中訪問我的動作類中的表單數據在Struts Action類中訪問JSP表單數據

login.jsp的我的第一個應用程序:

<div class="well"> 
    <form id="loginForm" method="POST" action="hr/login/" novalidate="novalidate"> 
     <div class="form-group"> 
      <div class="input-group"> 
       <span class="input-group-addon" id="UserEmail"><i class="fa fa-user" title="Enter Your username"></i></span> 
       <input type="text" class="form-control" id="username" name="username" value="" required title="Please enter you username" placeholder="Enter Username" /> 
      </div> 
      <span class="help-block"></span> 
     </div> 
     <div class="form-group"> 
      <div class="input-group"> 
       <span class="input-group-addon" id="UserPasswordMatch"><i class="fa fa-lock" title="Choose password"></i></span> 
       <input type="password" class="form-control" id="passwordmatch" name="passwordmatch" value="" required title="Enter your password" placeholder="Enter Password" /> 
      </div> 
     </div> 
     <button type="submit" class="btn btn-success btn-block">Login</button> 
    </form> 
</div> 

BookingAction.java:

public class BookingAction { 
    private String name; 

    HotelReservationServceImpl service = new HotelReservationServceImpl(); 

     public String execute() throws Exception { 

      return "success"; 
     } 

     public String loginExecute() 
     { 


      return "success"; 
     } 
     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 
} 

我也有一個User類私有屬性,其中包括usernamepassword與GE tters和setters。

service.java:

public class HotelReservationServceImpl implements IHotelReservationService { 


    HotelReservationDAOImpl dbcon = new HotelReservationDAOImpl(); 
    @Override 
    public int login(String username, String passwrd) { 
     if(username.isEmpty() || passwrd.isEmpty()) 
     { 
      System.out.print(" Enter username and password "); 

     } 
     else 
     { 
     int i = dbcon.login(username, passwrd); 

     } 


     //dbcon.dbConnector(); 
     return 0; 
    } 
} 

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> 
<constant name="struts.devMode" value="true" /> 
    <package name="helloworld" extends="struts-default"> 

     <action name="hello" 
      class="com.reservation.action.BookingAction" 
      method="execute"> 
      <result name="success">/HelloWorld.jsp</result> 
     </action> 
     <action name="login" 
     class="com.reservation.action.BookingAction" 
      method="loginExecute"> 
      <result name="success">/HelloWorld.jsp</result> 
     </action> 
    </package> 
</struts> 
+0

使用Struts的形式和標籤的輸入欄,其中的可能。將表單控件綁定到您的模型的動作bean。查看開發者控制檯是否有錯 –

回答

0

使用Struts標籤的形式綁定到動作和輸入字段操作屬性。

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

<s:form id="loginForm" method="POST" action="login" novalidate="novalidate">  
    <div class="form-group"> 
     <div class="input-group"> 
      <span class="input-group-addon" id="UserEmail"><i class="fa fa-user" title="Enter Your username"></i></span> 
      <s:textfield cssClass="form-control" id="username" name="user.username" value="" required title="Please enter you username" placeholder="Enter Username" /> 
     </div> 
     <span class="help-block"></span> 
    </div> 
    <div class="form-group"> 
     <div class="input-group"> 
      <span class="input-group-addon" id="UserPasswordMatch"><i class="fa fa-lock" title="Choose password"></i></span> 
      <s:password cssClass="form-control" id="passwordmatch" name="user.password" value="" required title="Enter your password" placeholder="Enter Password" /> 
     </div> 
    </div> 
    <button type="submit" class="btn btn-success btn-block">Login</button> 
</s:form> 

要綁定表單,您應該將操作名稱設置爲表單標記。要綁定輸入字段,您應該將屬性名稱設置爲struts輸入標記。

屬性位於User bean中,您應該將其聚合到操作類。

private User user; 
public User getUser() { return user; } 
public void setUser(User user) { this.user = user; } 

如果你不想產生額外的HTML頁面使用

<constant name="struts.ui.theme" value="simple"/>