Shivayan Mukherjee
Shivayan Mukherjee

Reputation: 827

how to show fetched values from database in spring <form:input>?

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!

Upvotes: 0

Views: 3473

Answers (3)

Shivayan Mukherjee
Shivayan Mukherjee

Reputation: 827

This is my code,please have a look and say what i might be doing wrong, 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>

Upvotes: 0

Chirag Kathiriya
Chirag Kathiriya

Reputation: 427

Please first retrieve the list from the datebase and set the list on the model attribute in the controller see the example set the

@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";
    }
}

your jsp page

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

Upvotes: 1

Ashok kumar
Ashok kumar

Reputation: 435

I guess you are expecting something like this.

//Assumes you have the following in your class

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

In the HTML would be.

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

For more details about click http://www.javacodegeeks.com/2013/07/spring-mvc-form-handling-vol-5-select-option-options-tags.html

Upvotes: 1

Related Questions