2015-05-24 92 views
3

我是使用struts的新手。我如何將表單輸入值傳遞給Action(struts 1)

當我提交表單時,我需要將表單值傳遞給一個操作。我想用輸入標籤 no html:text。

怎麼辦?

這是我的代碼:

形式JSP:

<form class="form-horizontal" action="address.do" method="post"> 
    Name: <input type="text" class="form-control" name="name"><br> 
    City <input type="text" class="form-control" name="city"><br> 
    Country: <input type="text" class="form-control" name="country"><br> 
    <button class="btn btn-success" type="submit">Submit</button> 
</form> 

的struts-config.xml:

<form-beans> 
    <form-bean name="myFrom" type="com.form.MyForm"/> 
</form-beans> 

<global-forwards> 
    <forward name="pagAddress" path="/address.do"/> 
</global-forwards> 

<action-mappings> 
    <action path="/address" 
      type="com.action.MainAction" 
      name="myForm"    
      scope="request" 
      input="/addressInput.jsp" 
      validate="true"> 
     <forward name="success" path="/addressInput.jsp"/> 
    </action> 
</action-mappings> 

的ActionForm:

public class MyForm extends ActionForm{ 

private static final long serialVersionUID = -XXXXXXXXXXXXXXXXXXXX; 
private String name = ""; 
private String city = ""; 
private String country = ""; 

public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getCity() { 
    return city; 
} 
public void setCity(String city) { 
    this.city = city; 
} 
public String getCountry() { 
    return country; 
} 
public void setCountry(String country) { 
    this.country = country; 
} 

@Override 
    public void reset(ActionMapping mapping, HttpServletRequest request) { 
     this.name = null; 
     this.city = null; 
     this.country = null; 
     super.reset(mapping, request); 
    } 

} 

行動:

public class MainAction extends Action { 

    public ActionForward execute(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 
     if(getErrors(request) == null ||getErrors(request).size() == 0) 
      return mapping.findForward("success"); 
     else 
      return mapping.getInputForward(); 

    } 
} 
+0

你有什麼樣的錯誤?你能更好地描述你的問題是什麼以及爲什麼這個解決方案不起作用? –

+0

當我在輸入中輸入值時,我需要將這些值傳遞給action,之後,當我提交頁面時,我需要將值輸入到適當的字段中。 –

+0

你應該在doPost menthod中寫下代碼 –

回答

1

要訪問一個Struts 1個動作形式值,則需要投ActionForm form到形式的該頁面使用,你的情況MyForm類型。然後你可以像正常一樣訪問它的getters。例如:

public ActionForward execute(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 

    MyForm theForm = (MyForm) form; 
    String name = theForm.getName(); 

    if (name == null || "".equals(name)) { 
     // error case; name is required 
     // do something 
    } 

    if(getErrors(request) == null ||getErrors(request).size() == 0) 
     return mapping.findForward("success"); 
    else 
     return mapping.getInputForward(); 

} 

如果您遇到越來越價值觀出MyForm到JSP頁面,你真的不能使用標籤庫的問題*,那麼你可以得到這樣的形式:

<% 
MyForm theForm = (MyForm) session.getAttribute("MyForm"); 
%> 

然後將其插入到頁面這樣的:

<form class="form-horizontal" action="address.do" method="post"> 
    Name: <input type="text" value="<%= theForm.getName() %>" class="form-control" name="name"><br> 
    City <input type="text" value="<%= theForm.getCity() %>" class="form-control" name="city"><br> 
    Country: <input type="text" value="<%= theForm.getCountry() %>" class="form-control" name="country"><br> 
    <button class="btn btn-success" type="submit">Submit</button> 
</form> 

你大概可以使用taglibs,只是不知道如何添加css類到他們。在標籤庫中,styleClass在輸出html中呈現爲class。試試這個:

<html:form styleClass="form-horizontal" action="address.do" method="post"> 
    Name: <html:text styleClass="form-control" name="name" /><br> 
    City <html:text styleClass="form-control" name="city" /><br> 
    Country: <html:text styleClass="form-control" name="country" /><br> 
    <button class="btn btn-success" type="submit">Submit</button> 
</html:form> 
相關問題