2011-03-24 81 views
2

我想使用彈簧窗體控制器綁定輸入類型複選框,但是我失敗了。帶有html複選框的彈簧窗體控制器

在這裏,我張貼控制器,豆和jsp的例子,還有一件事是我不能使用 。

下面是代碼:

控制器:

package com.test.web; 

import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.SimpleFormController; 

import com.vaannila.domain.User; 
import com.vaannila.service.UserService; 

@SuppressWarnings("deprecation") 
public class UserController extends SimpleFormController { 

    private UserService userService; 

    public UserController() { 
     setCommandClass(User.class); 
     setCommandName("user"); 
    } 

    public void setUserService(UserService userService) { 
     this.userService = userService; 
    } 

    @Override 
    protected ModelAndView onSubmit(Object command) throws Exception { 
     User user = (User) command; 
     user.setCommunity(user.getCommunity()); 
     userService.add(user); 
     return new ModelAndView("userForm","user",user); 
    } 

} 

的jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!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>Registration Page</title> 
<script> 
function submitForm(){ 
document.testForm.submit(); 

} 
</script> 
</head> 
<body> 

<form:form method="POST" commandName="user" name="testForm" action="./userRegistration.htm"> 
    <table> 
     <tr> 
      <td>User Name :</td> 
      <td><form:input path="name" /></td> 
     </tr> 
     <tr> 
      <td>Password :</td> 
      <td><form:password path="password" /></td> 
     </tr> 
     <tr> 
      <td>Gender :</td> 
      <td><form:radiobutton path="gender" value="M" label="M" /> 
       <form:radiobutton path="gender" value="F" label="F" /></td> 
     </tr> 
     <tr> 
      <td>Country :</td> 
      <td><form:select path="country"> 
       <form:option value="0" label="Select" /> 
       <form:option value="1" label="India" /> 
       <form:option value="2" label="USA" /> 
       <form:option value="3" label="UK" /> 
      </form:select></td> 
     </tr> 
     <tr> 
      <td>About you :</td> 
      <td><form:textarea path="aboutYou" /></td> 
     </tr> 
     <tr> 
      <td>Community :</td> 

       <td><input type="checkbox" name="community" value="Hibernate"/>Hibernate</br> 
       <input type="checkbox" name="community" value="test"/>test</br> 
       <input type="checkbox" name="community" value="test1"/>test1</br> 
       </td> 
     </tr> 
     <tr> 
      <td></td> 
      <td><form:checkbox path="mailingList" 
       label="Would you like to join our mailinglist?" /></td> 
     </tr> 
     <tr> 
      <td colspan="2"><input type="submit" onclick="submitForm();"></td> 
     </tr> 
    </table> 

</form:form> 

</body> 
</html> 

的Java bean:

package com.test.domain; 

public class User { 

    private String name; 
    private String password; 
    private String gender; 
    private String country; 
    private String aboutYou; 
    private String[] community; 
    private Boolean mailingList; 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String getGender() { 
     return gender; 
    } 
    public void setGender(String gender) { 
     this.gender = gender; 
    } 
    public String getCountry() { 
     return country; 
    } 
    public void setCountry(String country) { 
     this.country = country; 
    } 
    public String getAboutYou() { 
     return aboutYou; 
    } 
    public void setAboutYou(String aboutYou) { 
     this.aboutYou = aboutYou; 
    } 
    public String[] getCommunity() { 
     return community; 
    } 
    public void setCommunity(String[] community) { 
     this.community = community; 
    } 
    public Boolean getMailingList() { 
     return mailingList; 
    } 
    public void setMailingList(Boolean mailingList) { 
     this.mailingList = mailingList; 
    } 


} 

我嘗試不同的方法,但沒有運氣。請提供任何提示。

+0

日誌告訴你什麼? – heldt 2011-03-24 22:26:34

+0

@holdt:它沒有給出任何異常或錯誤。當我檢查複選框並提交表單控制器時,它沒有綁定值,我提交給同一頁面。在這裏我放棄了複選框。 – rajputhch 2011-03-24 22:28:43

+0

@Bozho:你編輯了什麼? – rajputhch 2011-03-24 23:37:37

回答

1

如果您不使用表單標籤,它不會自動綁定您的複選框。如果你使用純html,你必須綁定你的自我。

你可以通過添加一個社區對象列表來解決這個問題,然後使用form:checkboxes。 例如:

<form:checkboxes path="communityList" items="${communityList}" itemValue="key" itemLabel="value" /> 

我也是會建議你使用的ModelAndView這樣時使用HashMap:

Map<String, Object> model = new HashMap<String, Object>(); 
model.put("user", user); 
model.put("communityList", communityList); 
return new ModelAndView("userFormat", model); 

手動綁定使用 'ServletRequestUtils' ... http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/ServletRequestUtils.html

public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException { 
Long subscriptionOwnerId = ServletRequestUtils.getLongParameter(request, "id"); 
return new ModelAndView('test'); }`  
+0

是的,我知道這一點,但不幸的是我不能使用窗體:checkboxes.Because根據我的要求,基於一個複選框選擇,我應該使用ajax填充其他複選框。在這我必須使用輸入類型= checkbox.How我可以手動綁定這個嗎? – rajputhch 2011-03-24 22:56:36

+0

這個人的任何提示。 – rajputhch 2011-03-28 20:40:12

+0

新增手動綁定示例 – heldt 2011-03-29 06:54:51

8

瀏覽器不會發送如果複選框未被選中,請求中的字段。該值將爲「true」或不發送。你永遠不會得到「錯誤」的價值。

添加一個隱藏字段_name每一個複選框

EX:
<input type="checkbox" name="community" value="Hibernate"/>
<input type="hidden" name="_community" value="on"/>

然後,春天會照顧它。

+0

謝謝先生,您救了我的一天。 – 2013-03-13 13:52:31

+0

很高興幫助:) – 2013-03-14 03:43:52