1

I want to update some user’s data and have issue with receiving parameters from JSP dropdown menu. I want to receive entered compId from “Enter PC” block and pass it as a PathVariable. But it is not seen. If I hardcode action="${app}/adminEdit.do/${user.userId}/${any number}" it works. So, question is – now to get this parameter from dropdown and set it to path? Thanks in advance.

Update.jsp snippet

<c:set var="app" value="${pageContext.request.contextPath}"/>
............
<DIV class="admin_redaction_block">
<sf:form name="adminUserUpdate"
         method="POST"
         modelAttribute="userForm"
         action="${app}/adminEdit.do/${user.userId}/${comp.compId}"
         enctype="application/x-www-form-urlencoded">

    <c:if test="${not empty errorMsg}">
        <div class="error">
            <c:out value="${errorMsg}"/>
        </div>
    </c:if>

    <sf:label path="password"><strong>Enter new password:</strong></sf:label> <br>
    <sf:input path="password" type="text" size="20"/><br>
    <sf:errors path="password" cssClass="error"/>
    <br>
    <sf:label path="email"><strong>Enter new Email:</strong></sf:label> <br>
    <sf:input path="email" type="text" size="20"/><br>
    <sf:errors path="email" cssClass="error"/>

    <strong>PC Assigned:</strong>
    <h3 class="h3">
        <td>
            <c:choose>
                <c:when test="${user.computers!= null && !user.computers['empty']}">
                    <c:forEach items="${user.computers}" var="comp">
                        <c:out value="${comp.pcName}"/>
                    </c:forEach>
                </c:when>
                <c:otherwise>
                    <p class="h3_error">No PC Assigned</p>
                </c:otherwise>
            </c:choose>
        </td>
    </h3>

    <sf:label path="computers">Enter PC:</sf:label> <br>
    <sf:select path="computers" size="3">
        <c:forEach items="${computers}" var="comp">
            <sf:option value="${comp.compId}">
                <c:out value="${comp.compId}"/>
            </sf:option>
        </c:forEach>
    </sf:select>
    <br> <br>

    <input type="SUBMIT" name="SUBMIT" value="Update User"/>
</sf:form>

Controller

 @RequestMapping(value = "/adminEdit.do/{userId}/{compId}", method = RequestMethod.POST)
 public ModelAndView updateUserProcess(@ModelAttribute(value = "userForm")
 UserForm userForm,
 @PathVariable("userId") Integer userId,
 @PathVariable("compId") Integer compId,
         BindingResult result, Model model,
         HttpSession session,
         HttpServletRequest request) {
 User user = userService.getUserById(userId);
 model.addAttribute("computers", computerService.getAllComputers());
 ............
 model.addAttribute("userForm", userForm);
 return updatingUser(user, model, userForm);
 }  
9
  • 1
    What type of error you are getting ? and If I hardcode action="${app}/adminEdit.do/${user.userId}/${any number}" it works. -what does it mean ? Commented Jul 10, 2014 at 9:20
  • I get 404 error, because path is wrong localhost:8080/adminEdit.do/2/ - there are no second PathVariable. About hardcode - it means, that if I set value with myself I get right path localhost:8080/adminEdit.do/2/5 (or any number) and it means that to the controller is passed TWO variables. Hope it is clearer now. Commented Jul 10, 2014 at 9:31
  • Means your issue is not passing 2nd parameter correctly, it is blank.. Commented Jul 10, 2014 at 9:33
  • Can you tell us what is user and comp here? Commented Jul 10, 2014 at 9:37
  • In your first combo ${user.computers} is there and in second ${computers} Commented Jul 10, 2014 at 9:37

2 Answers 2

2

You cannot.

You simply forgot that thing are written at different time.

<sf:form name="adminUserUpdate" ...
     action="${app}/adminEdit.do/${user.userId}/${comp.compId}" ...>

is written at the time of answering the request that generates the form. At that time, your app (server side) is simply generating a HTML page, and the $comp.compid} does not exist. You can verify it by looking at the HTML source code of the page in your browser.

Later, when you click on the submitbutton, the browser gather data from input fields encode all and send it via a POST request to the action URL without changing it. Browser does not even know that you wrote ${app}/adminEdit.do/${user.userId}/${comp.compId} in your jsp : it only recieved a plain text string localhost:8080/adminEdit.do/2/

So ... try to get comp.compid from an input field of your form using a <sf:select> or <sf:checkboxes> tag.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I tried to set values to the FORM too but they somehow are not setted. Well, thank you very much. I'll work in this way.
2

Well, after long time of searching I've found now I can pass parameters from JSP to Controller. There are special class CustomCollectionEditor which helps pass even multiple select values. Here is good example https://blog.codecentric.de/en/2009/07/multiple-selects-mit-spring-mvc-2/

And my snippet:

 @InitBinder("userForm")
    private void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Set.class, "computers", new CustomCollectionEditor(Set.class) {

            @Override
            protected Object convertElement(Object element) {
                String pcName = null;
                Set<Computer> computerSet = new LinkedHashSet<>();    
                if (element instanceof String && !((String) element).equals("")) {
                    pcName = (String) element;                       
                }                   
                return pcName != null ? computerService.getComputerByName(pcName) : null;
            }
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.