2014-05-16 2 views
1

첫 번째 Spring MVC 애플리케이션을 구축 중이며 조금 갇혀있다. 숫자 (지역) 목록을 검색하고 목록 상자에 채우는 데이터베이스 호출이 있습니다. 사용자는 선택 항목 중 하나를 선택하고 제출 버튼을 눌러야합니다. 문제는 컨트롤러 측에서 이것을 캡처하는 방법을 모른다는 것입니다. 처음에는 매개 변수를 얻는 것만 큼 쉽 겠지만 생각하지 않았습니다. 나는 자바 클래스/빈을 단지 숫자로 만들고 싶지 않다. 그것은 어리석은 것처럼 보일 것입니다. 그럼 내 코드를 알려주지.Spring의 ListBox에서 사용자 선택하기 MVC/JSP/Controller

JSP :

<html> 
<head> 
    <title>Spring 3 MVC Series - Contact Manager</title> 
</head> 
<body> 
<h2>Branches</h2> 

<form:form method="get" action="branch.html"> 
    <table> 

    <c:if test="${not empty lists}"> 
<select name="regions" size="4"> 
<c:forEach var="listVal" items="${lists}"> 

    <option>${listVal} </option> 
</c:forEach> 
</select> 

</c:if> 
</table> 
</form:form> 

<form:form method="get" action="loadbranches.html"> 
    <c:if test="${not empty lists}"> 
<select name="branches" size="4"> 
<c:forEach var="branchVal" items="${branches}"> 

    <option>${branchVal} </option> 
</c:forEach> 
</select> 

</c:if> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="submit choice" /> 
     </td> 
    </tr> 
</form:form> 

</body> 
</html> 

CONTROLLER :

@Controller 
@SessionAttributes 
public class BranchController { 

    @RequestMapping(value = "/loadbranches", method = RequestMethod.GET) 
    public ModelAndView getBranches() 
    { 

     //org.springframework.web.util.WebUtils.getParametersStartingWith(, prefix) 
     DatabaseConnect dbCon = new DatabaseConnect(); 

     ArrayList<Object> branches = dbCon.loadData(USER SELECTED NUMBER I CANNOT FIGURE OUT); 

     ModelAndView model = new ModelAndView("branch"); 

     model.addObject("branch", branches); 


     return model; 
    } 

    @RequestMapping("/branch") 
    public ModelAndView showRegions() 
    { 

      DatabaseConnect dbCon = new DatabaseConnect(); 

      ArrayList<Object> regionIDS = dbCon.loadData("regions"); 

      ModelAndView model = new ModelAndView("branch"); 
      model.addObject("lists", regionIDS); 

      return model; 

    } 

} 

당신이 아직은 정말 응용 프로그램의 복잡 아니다시피. 데이터베이스와 다른 Java 클래스로 작업하지만 완벽하게 작동합니다. 그래서 요청 매개 변수 또는 뭔가 목록 상자에서 사용자 선택을 얻는 방법은 무엇입니까? 이상적으로 나는 사용자가 제출을 명중 할 때 이것이 일어나기를 바란다. 그래서 이것이 의미가있는 경우 숫자 목록 (영역)의 초기로드 외에 별도의 양식 태그가있는 이유이다. 선택 경로 매개 변수와 관련된 여러 가지 예제를 살펴 보았지만 사용자가 제출 버튼을 클릭 한 후 목록 상자에서 선택 항목을 가져 오는 쉬운 방법이 아닙니까? 나는 이것에 도움을 정말로 바랄 것이다!

감사합니다.

+0

HTML 양식이 반환 유형은 무엇입니까? 컨트롤러에'Integer' 또는'String'을 넣을 수 있습니다. – chrylis

답변

2

이 함께 시도 : 사용자 (복수 선택) 1 개 이상의 brach를 선택할 수 있습니다 당신이 목록 삽입 할 수

@RequestMapping(value = "/loadbranches", method = RequestMethod.GET) 
public ModelAndView getBranches(@RequestParam(value="branches") String branches) 
    { 

     .... 
    } 

경우 :

@RequestMapping(value = "/loadbranches", method = RequestMethod.GET) 
public ModelAndView getBranches(@RequestParam(value="branches") List<String> branches) 
     { 

      .... 
     }