0

I need to show the fetched values from database which are stored in an arraylist using spring form:input tag. However i found that the 'value' attribute isn't supported. Please help!

4

3 回答 3

1

请首先从数据库中检索列表并在控制器中的模型属性上设置列表参见示例设置

@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>
于 2013-10-03T06:01:36.217 回答
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的更多详细信息

于 2013-10-03T05:42:39.030 回答
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>
于 2013-10-03T12:55:56.093 回答