2017-11-30 4 views
1

간단하게 등록했습니다. 여기 컨트롤러두 콩의 데이터를 JSP로 가져 오기

<form class="form-horizontal" method="post" action="RightsGroupRegister"> 
     <div class="form-group"> 
      <label for="Name of group" class="col-sm-2 control-label">Name of group:</label> 
      <div class="col-sm-10"> 
       <input class="form-control" id="name" name="name" placeholder="Name of group"> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label for="Rights" class="col-sm-2 control-label">Rights:</label> 
      <div class="col-sm-10"> 
       <input class="form-control" id="rights" name="rights" placeholder="Rights"> 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-10"> 
       <button class="btn btn-default">Create</button> 
      </div> 
     </div> 
    </form> 

그리고 :

@Controller 
public class RightsGroupController { 
private static final Logger LOGGER = LoggerFactory.getLogger(RightsGroupController.class); 

private final RightsGroupService rightsGroupService; 

@Inject 
public RightsGroupController(RightsGroupService rightsGroupService) { 
    this.rightsGroupService = rightsGroupService; 
} 

@RequestMapping(value = "RightsGroupRegister", method = RequestMethod.GET) 
public ModelAndView getRightsGroupView() { 

    LOGGER.debug("Received request for addRight"); 
    return new ModelAndView("RightsGroupRegister", "form", new RightsGroupForm()); 
} 

@RequestMapping(value = "RightsGroupRegister", method = RequestMethod.POST) 
public String createRightsGroup(@ModelAttribute("form") RightsGroupForm form) { 
    LOGGER.debug("Received request to create {}, with result={}", form); 

    try { 
     rightsGroupService.save(new Rightsgroup(form.getName(),form.getRights())); 
    } catch (RightsGroupAlreadyExistsException e) { 
     LOGGER.debug("Tried to create rightsgroup with existing id", e); 
     return "right_create"; 
    } 
    return "redirect:/"; 
}} 

지금 문제, 정말 그것을 작동하는 방법을 이해 해달라고 여기

내 JSP 레지스터 페이지의 일부입니다. 다른 개체에서이 양식 데이터를 가져 오는 방법은 무엇입니까? 예를 들어 권리 (id, name)의 목록?

+0

무엇을 의미합니까? 'RightsGroupForm'의 필드에 접근하고 싶습니까? 당신이 이미 그것을하고있는 것처럼 보입니다. –

+0

개체에서 데이터를 채우기 표 또는 콤보 상자를 채우려면 오른쪽 –

+0

해결 했습니까? 내 안사가 도와 줬어? –

답변

0

이렇게하는 데는 몇 가지 방법이 있습니다. 게시 방법에서 리디렉션 할 페이지의 데이터에 액세스하려는 경우를 가정합니다. 포스트 방법에 인수에

public String createRightsGroup(@ModelAttribute("form") RightsGroupForm form, Model model)

추가 모델이 하나의 방법입니다. 그런 다음, 즉 :

model.addAttribute("name", form.getName()); 
model.addAttribute("rights", form.getRights()); 

하고 .. 당신의 JSP (right_create.jsp)에서

return "redirect:/right_create"; 

같은 액세스 권한 필드 :

<table> 
    <tr> 
    <td>${name}</td> 
    <td>${rights}</td> 
    </tr> 
</table> 

모음을 경우 Right 경우 당신이 할 수있는 해야 할 일 :

<table> 
<c:forEach items="${rights}" var="right"> 
    <tr> 
     <td>${right.id}</td> 
     <td>${right.name}</td> 
    </tr> 
</c:forEach> 
</table> 
관련 문제