如何在spring中显示从数据库获取的值<form:input>?</form:input>

时间:2013-10-03 05:23:58

标签: java spring spring-mvc arraylist

我需要显示数据库中的获取值,这些值使用spring form:input tag存储在arraylist中。但是我发现不支持'value'属性。请帮忙!

3 个答案:

答案 0 :(得分:1)

我猜你期待这样的事情。

//假设您的课程中有以下内容

    public class Students{
      private String name;
      private List<String> Departments;
     /* getters/setters */
    }

在HTML中会是。

    <form:input path="departments[0]" />
    <form:input path="departments[1]" />

有关点击http://www.javacodegeeks.com/2013/07/spring-mvc-form-handling-vol-5-select-option-options-tags.html

的详细信息

答案 1 :(得分:1)

请先从数据库中检索列表并在控制器中的model属性上设置列表,请参阅示例设置

@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>

答案 2 :(得分:0)

这是我的代码,请看看我可能做错了什么,    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>