【问题标题】:Sending a parameter from List to a method which is annotated with @ModelAttribute将参数从 List 发送到使用 @ModelAttribute 注释的方法
【发布时间】:2016-10-14 14:01:30
【问题描述】:

我有一个如下所示的列表页面,当我单击“编辑”按钮时,我想将 patientId 发送到 getObject(),这样如果 patientId 存在,我就可以从 DB 加载对象。我尝试了很多方法,但它在getObject()中将null作为patiendId。 任何建议,将不胜感激。

以下是我的列表页面::

       <c:forEach var="pat" items="${patients}" varStatus="status">
            <tr>                            
            <c:url var="editUrl" value="/patient/${pat.patientId}"/>

                    <td>${pat.firstName}</td>
                    <td>${pat.mobileNumber1}</td>
                    <td>${pat.emailId1}</td>

               <td><a href='<c:out value="${editUrl}"/>'>Edit</a></td>

        </tr>
    </c:forEach>

控制器代码::

@Controller
@RequestMapping(value="/patient")
public class PatientController {

  @ModelAttribute
  public Patient getObject(@RequestParam(required=false) String patientId){

    System.out.println("Model Attribute method :: "+patientId);
    //Here I want to load the object using patientId.
    return(patientId != null ? patientDAO.findPatientById(patientId) : new    Patient());

}

@RequestMapping(value="/{patientId}", method=RequestMethod.GET)
public String editPatient(@PathVariable("patientId")String  patientId){
    System.out.println("Editing Id : "+patientId);
    //Able to get the Id here.
    return "editPage";

}

【问题讨论】:

    标签: spring modelattribute


    【解决方案1】:

    您在一个地方使用@PathVariable,在另一个地方使用@RequestParam,并且正在发送一个路径变量(/patients/123,而不是请求参数/patients?patientId=123):使用其中一个。

    如下更新应该可以:

      @ModelAttribute
      public Patient getObject(@PathVariable(required=false) String patientId){
    
        System.out.println("Model Attribute method :: "+patientId);
        //Here I want to load the object using patientId.
        return(patientId != null ? patientDAO.findPatientById(patientId) : new    Patient());
    
    }}
    

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

    【讨论】:

    • 嗨,谢谢你的回答,但在我的情况下,因为许多其他方法都在调用 getObject() 而不会通过@pathvariable,你能帮我如何从列表中发送 requestParam。谢谢
    • 那么你为什么不问一个正确的问题给出完整的事实,然后没有其他人会浪费时间试图回答。
    • 我是这么想的,对不起:-(
    【解决方案2】:

    我得到了答案。下面是我的工作代码。希望它会帮助某人。谢谢大家。

    <tr>                                                        
        <c:url var="deleteUrl" value="/patient/delete/${pat.patientId}"/>
    
                <td>${pat.firstName}</td>
                <td>${pat.mobileNumber1}</td>
                <td>${pat.emailId1}</td>
    
            <td><a href="newPatient?patientId=${pat.patientId}">Edit</a></td>
            <td><a href='<c:out value="${deleteUrl}"/>'>Delete</a></td>
    
    </tr>
    </c:forEach>
    

    控制器代码::

    @ModelAttribute
    public Patient getObject(@RequestParam(value="patientId", required=false) String patientId){
        System.out.println("Model Attribute method :: "+patientId);
    
        return (patientId != null ? patientDAO.findPatientById(patientId) : new Patient());     
    }
    
    @RequestMapping(value="/newPatient", method=RequestMethod.GET)
    public String demo(ModelMap model){
    
        System.out.println("Demo of Patient");  
    
        return "newPatient";
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-25
      • 2017-08-03
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2018-05-11
      • 2015-02-19
      相关资源
      最近更新 更多