2017-11-30 13 views
1

私は単純に登録しています。ここ2つのbeanからjspにデータを取得

は私の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:/"; 
}} 

さて問題は、私は実際にそれで動作する方法を理解しません。 このフォームデータを別のオブジェクトから取得するにはどうすればよいですか?権利のリスト(id、name)?

+0

どういう意味ですか?あなたは 'RightsGroupForm'のフィールドにアクセスしたいですか?あなたはすでにそれをやっているように見えます。 –

+0

オブジェクトからのデータで表またはコンボボックスを塗りたい –

+0

あなたはそれを解決しましたか?私のアンサーは助けましたか? –

答えて

0

これにはいくつかの方法があります。私はあなたのポスト方法でリダイレクトしているページのデータにアクセスすることを意味すると思います。これは一つの方法である:

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

は、POSTメソッドでは、あなたの引数にモデルを追加します。その後、すなわち:あなたのJSP(right_create.jsp)で

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

と..

return "redirect:/right_create"; 

あなたのようなフィールドへのアクセス:あなたができるRight場合は、コレクションを持っている場合

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

をdo:

<table> 
<c:forEach items="${rights}" var="right"> 
    <tr> 
     <td>${right.id}</td> 
     <td>${right.name}</td> 
    </tr> 
</c:forEach> 
</table> 
関連する問題