2013-10-03 14 views

回答

1

請首先檢索從DATEBASE列表中,並設置在控制器中的模型屬性列表中看到的例子設置

@Controller 
public class UserController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String userHome(Model model, EventBean event, UserService userService,ImageBean image) 
    { 
     List<Event> events = userService.viewNews(); //retrieve the list from datebase 
       model.addAttribute("event", event); //add bean object 
     model.addAttribute("events", events); //add list in model attribute 
     return "home"; 
    } 
} 

你的jsp頁

<form:form modelAttribute="event"> <!--set the model attribute here --> 
     <form:input path="news" value="${events.get(0).news}" /> 
</form:form> 
+0

是的ModelAttribute相同命令名? –

+0

是啊沒有區別 –

+0

我嘗試訪問使用上述技術的元素,但我面臨一個問題,諸如名稱,電子郵件等字段被包裝在父bean中。在這種情況下,訪問這些元素的語法應該是什麼?我得到這個異常「org.apache.jasper.JasperException:/WEB-INF/views/EditProfile.jsp(31,3)當沒有指定默認命名空間時,函數get必須與前綴一起使用」 –

0

這是我的代碼,請看看和s唉我可能是做錯了, JAVA


public ModelAndView userEditProfile(@ModelAttribute("userDetails") UserFormbean registration,BindingResult result,HttpServletRequest request){ 

     ModelAndView mav=null; 
     HttpSession httpSession=null; 
     List userProfileList=new ArrayList(); 
     httpSession=request.getSession(); 
     if (httpSession != null) { 
     UserFormbean formbean=(UserFormbean)httpSession.getAttribute("UserRegistrationFormBean"); 
    userProfileList= userRegistrationService.getUserProfileInfo(formbean); 
    mav=new ModelAndView("EditProfile"); 
    mav.addObject("userProfileInfoList", userProfileList); 


    } 
    return mav; 

    } 

JSP:: 
----- 
<c:if test="${not empty userProfileInfoList}"> 
<c:forEach var="temp" items="${userProfileInfoList}"> 



     <div> 
     <form:label path="userRegistration.email"><spring:message code="label.email"/></form:label> 
     <form:input path ="userRegistration.email" value="${temp.get(0).UserRegistration.email}"/> 
     <form:errors path="userRegistration.email"/> 
     </div> 

     <div> 
     <form:label path="userRegistration.firstName"><spring:message code="label.firstname"/></form:label> 
     <form:input path ="userRegistration.firstName" value="${temp.get(0).UserRegistration.firstName}"/> 
     <form:errors path="userRegistration.firstName"/> 
     </div> 


     <div> 
     <form:label path="userRegistration.lastName"><spring:message code="label.lastname"/></form:label> 
     <form:input path ="userRegistration.lastName" value="${temp.get(0).UserRegistration.lastName}"/> 
     <form:errors path="userRegistration.lastName"/> 
    </div> 


    </c:forEach> 
    </c:if> 
+0

我做到了這一點,它的工作。

現在有人可以告訴我爲什麼這個工作和上面的代碼沒有? –

相關問題