2011-03-24 7 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> 

자바 콩 : 나는 다른 방법을 시도

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

+0

@ heldt : 예외 나 오류를주지 않습니다. 확인란을 선택하고 양식 컨트롤러를 제출할 때 값을 바인딩하지 않습니다. 동일한 페이지로 제출합니다. 여기에 확인란이 누락되었습니다. – rajputhch

+0

@ 보 소 : 편집 한 내용? – rajputhch

답변

1

양식 태그를 사용하지 않으면 확인란이 자동으로 바인딩되지 않습니다. 일반 HTML을 사용하는 경우 자신을 바인딩해야합니다.

커뮤니티 객체 목록을 추가 한 다음 form : checkbox를 사용하여이를 해결할 수 있습니다.

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

: 나는이 같은의 ModelAndView를 사용하는 경우는 HashMap을 사용하도록 권 해드립니다 것이다

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

: 예를 들어

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

네,하지만이 불행히도 내가 사용할 수 없습니다 form : checkboxes.Because 내 요구 사항에 따라 하나의 확인란 선택을 기반으로, 내가 다른 확인란을 ajax.In 입력을 사용하여 확인란을 채워야한다 입력 = checkbox.How 내가 수동으로 바인딩 할 수 있습니다. – rajputhch

+0

이 사람에 대한 힌트. – rajputhch

+0

수동 바인딩에 대한 추가 예제 – heldt

8

브라우저는 확인란을 선택하지 않은 경우 요청의 값은 "true"이거나 전송되지 않습니다. 당신은 결코 "거짓"값을 얻지 못할 것입니다.

모든 체크 박스 _name와 숨겨진 필드를 추가

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

그런 다음, 봄이 처리됩니다.

+0

감사합니다. 내 하루를 저장했습니다. 다행히도 –

+0

:) –

관련 문제