Results 1 to 15 of 15
  1. #1
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Passing object from JSP page through JSTL into controller's method

    Hello everybody! Help me solve a problem. I need to pass advanced object into controller from JSP page, but I always get null result.
    It's a controller method:
    Java Code:
        @RequestMapping(value="admin-user-edit", method=RequestMethod.POST)
        public ModelAndView editUser(@ModelAttribute(value="user") UsersEntity user)
        {
    	 if (null == user)System.out.println("User is null");
    	 else
    	     System.out.println("User name = " + user.getName() + " | Users id = " + user.getId());
    	 ModelAndView view = new ModelAndView();
    	 return view;
        }
    And this is a JSP page snippet. I need to choose some user from user list and pass it to controller.
    XML Code:
    		<c:forEach var="user" items="${user_list}">
    			<tr>
    				<td><c:out value="${user.id}" /></td>
    				<td><c:out value="${user.name}" /></td>
    				<td><c:out value="${user.login}" /></td>
    				<td><c:out value="${user.status}" /></td>
    				<td><form action="admin-user-edit" method="post">
    						<input type="hidden" name="user" value="${ user }">
    						<c:set var="varr" value="${ user.name }" scope="request"></c:set>
    						<input type="submit" value="Edit">
    					</form></td> 
    			</tr>
    		</c:forEach>
    I tried to pass it through HttpServletRequest argument but all the same. Anybody can help me?
    Thank you for your time.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing object from JSP page through JSTL into controller's method

    Show what HTML that produces on your client.
    I doubt it's what you expect.

    And then you can explain exactly what the requirement is for this Edit button and we can go from there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing object from JSP page through JSTL into controller's method

    You don't have a form inside a form do you? I see you wrapped that edit link in its own unique form, that forEach is not already inside another form?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing object from JSP page through JSTL into controller's method

    Quote Originally Posted by gimbal2 View Post
    You don't have a form inside a form do you? I see you wrapped that edit link in its own unique form, that forEach is not already inside another form?
    I doubt it (but you could be right).
    It looks like a table, and the Edit button for each row is wrapped in a form, presumably in an attempt to send all the user data (for that row) to the server.

    But there are some assumptions there on my part, which is why I hope the OP will come back and go through their thinking.
    I'm pretty sure I know the answer, but it would be good if they could work through it, if nothing else so we can see where their misconceptions lie.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing object from JSP page through JSTL into controller's method

    Yeah I see what you're aiming at. Something about a hidden input field not holding what it should be holding.

    Possibly its both reasons too. Problems hiding other problems are not uncommon to me to be honest.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Passing object from JSP page through JSTL into controller's method

    No my foreach is not inside another form. Foreach obtains User entity from users list (It works good) and prints it into table. Every row has own form, that sends user object into controller.
    Here the full jsp fragment which I expect include in my page.
    Java Code:
    <%@ page contentType="text/html;charset=UTF-8" language="java"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
    <div>
    	<table>
    		<tr>
    			<td width="50">Id</td>
    			<td width="50">Name</td>
    			<td width="50">Login</td>
    			<td width="50">Status</td>
    		</tr>
    		<c:forEach var="user" items="${user_list}">
    			<tr>
    				<td><c:out value="${user.id}" /></td>
    				<td><a href="admin-user-edit?user=${ user }"><c:out
    							value="${user.name}" /></a></td>
    				<td><c:out value="${user.login}" /></td>
    				<td><c:out value="${user.status}" /></td>
    				<td><form action="admin-user-edit" method="post">
    						<input type="hidden" name="user" value="${ user }">
    						<input type="submit" value="Edit">
    					</form></td>
    			</tr>
    		</c:forEach>
    	</table>
    
    </div>
    It works when I try to pass user's id through href link by "get" method, when I change controller's method with Integer attribute

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing object from JSP page through JSTL into controller's method

    I asked for the HTML for one of those rows from the client.
    That is the most relevant thing when you are looking at what is going to happen when the user clicks the Edit button.

    And also explain what it is you are attempting to achieve.,
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Passing object from JSP page through JSTL into controller's method

    I expect to open edit user page by clicking "Edit button" on the page with displayed list of users.
    Every User's row has form with "Edit" button.
    And if I understood you correctly here the source html from w-browser.

    It is page with user list. In this case I have only 1 user.
    XML Code:
    <div>
    	<table>
    		<tr>
    			<td width="50">Id</td>
    			<td width="50">Name</td>
    			<td width="50">Login</td>
    			<td width="50">Status</td>
    		</tr>
    		
    			<tr>
    				<td>1</td>
    				<td><a href="admin-user-edit?user=1">Toha</a></td>
    				<td>kapitoha</td>
    				<td>asd</td>
    				<td><form action="admin-user-edit" method="post">
    						<input type="hidden" name="user" value="com.springapp.mvc.domain.UsersEntity@404ac870">
    						
    						<input type="submit" value="Edit">
    					</form></td>
    			</tr>
    		
    	</table>
    
    </div>
    Here the source of editor page:
    XML Code:
    <div>
               <form action="admin-user-save" method="post">
    		<label for="name">User Name </label> <input type="text"
    			value="" name="name" /> <label for="name">User
    			Login </label> <input type="text" value="" name="login" /> <label
    			for="name">User Password </label> <input type="text"
    			value="" name="login" />
    		<!-- Rules list -->
    		<ul>
    						<li><input type="checkbox">read</li>
    					
    						<li><input type="checkbox">edit_master</li>
    					
    						<li><input type="checkbox">edit_author</li>
    					
    						<li><input type="checkbox">create_article</li>
    					
    						<li><input type="checkbox">manage_user</li>
    			
    		</ul>
    		<input type="submit" value="Save" />
    	</form>
    
    	<form action="admin-users">
    		<input type="submit" value="Cancel">
    	</form>
    
    </div>
    And editor page in JSP shape:
    XML Code:
    <div>
    	<c:set var="user_obj" value="${ user }" />
    	<c:set var="userRightsList" value="${ user_obj.usersrulesById }" />
    	<form action="admin-user-save" method="post">
    		<label for="name">User Name </label> <input type="text"
    			value="${user_obj.name }" name="name" /> <label for="name">User
    			Login </label> <input type="text" value="${ user_obj.login }" name="login" /> <label
    			for="name">User Password </label> <input type="text"
    			value="${ user_obj.password }" name="login" />
    		<!-- Rules list -->
    		<ul>
    			<c:set var="field" value="id" />
    			<c:forEach var="rule" items="${ rules_list }">
    				<c:choose>
    					<c:when
    						test="${ coll:containsByField(userRightsList, field, rule.id) }">
    						<li><input type="checkbox" checked>${ rule.nameRule }</li>
    					</c:when>
    					<c:otherwise>
    						<li><input type="checkbox">${ rule.nameRule }</li>
    					</c:otherwise>
    				</c:choose>
    
    			</c:forEach>
    		</ul>
    		<input type="submit" value="Save" />
    	</form>
    
    	<form action="admin-users">
    		<input type="submit" value="Cancel">
    	</form>
    
    </div>
    And here full controller's method that handles request
    Java Code:
        @RequestMapping(value="/admin-user-edit", method=RequestMethod.POST)
        public ModelAndView editUser(@ModelAttribute(value="user") UsersEntity user)
        {
    	 if (null == user)System.out.println("User is null");
    	 else
    	     System.out.println("User name = " + user.getName() + " | Users id = " + user.getId());
    	 ModelAndView view = new ModelAndView("/admin-user-edit", "user", user);
    	 view.setViewName("/admin");
    	 view.addObject("page_tag", "user-edit");
    	 if (null != user && user.getId() > 0)
    	 {
    	     user = getUserRepository().getUser(user.getId());
    	 }
    	 else
    	 {
    	     user = new UsersEntity();
    	     user.setUsersrulesById(new HashSet<UsersrulesEntity>());
    	 }
    	 List<RulesEntity> rulesList = rulesRepo.getRulesList();
    	 view.addObject("user", user);
    	 view.addObject("rules_list", rulesList);
    	 return view;
        }
    Last edited by kattoha; 05-13-2015 at 01:30 PM.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing object from JSP page through JSTL into controller's method

    Java Code:
    <input type="hidden" name="user" value="com.springapp.mvc.domain.UsersEntity@404ac870">
    And this doesn't look wrong to you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing object from JSP page through JSTL into controller's method

    So, as gimbal shows, that is not doing what you expect.
    Looking at your code, you want the user selected.
    Since the user data is (somewhere) on the server, why attempt to send the whole user object?

    You have something that identifies a user, so why not send just that?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Passing object from JSP page through JSTL into controller's method

    Quote Originally Posted by gimbal2 View Post
    Java Code:
    <input type="hidden" name="user" value="com.springapp.mvc.domain.UsersEntity@404ac870">
    And this doesn't look wrong to you?
    Yes, it looks as a String, but I thought that in a nutshell there is a real object, and this case just shows us object's class. Is not?

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing object from JSP page through JSTL into controller's method

    Quote Originally Posted by kattoha View Post
    Yes, it looks as a String, but I thought that in a nutshell there is a real object, and this case just shows us object's class. Is not?
    No, its text in a HTML document.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing object from JSP page through JSTL into controller's method

    Quote Originally Posted by kattoha View Post
    Yes, it looks as a String, but I thought that in a nutshell there is a real object, and this case just shows us object's class. Is not?
    This is a common mistake.
    JSPs execute on the server, with the main purpose (frankly sole purpose) of generating HTML for display on the client.

    So you need to be thinking in terms of HTML, not Java, for what you want to do.
    What would be a sensible thing to send to the server for it to identify the UserEntity that is to be edited?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Passing object from JSP page through JSTL into controller's method

    Thank you for explanation.

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing object from JSP page through JSTL into controller's method

    I'd be a little concerned that whatever study material you're using to learn this apparently does not begin with the important fundamentals.

    I have always found that this free golden oldie makes a good distinction between what happens on the client and what happens on the server:

    Free Online Version of Core Servlets and JavaServer Pages (Second Edition) in PDF
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 02-08-2015, 04:01 AM
  2. Passing a class's object as parameter to a method...
    By darshanaprasad in forum New To Java
    Replies: 4
    Last Post: 12-15-2012, 10:07 PM
  3. Replies: 1
    Last Post: 10-27-2011, 07:21 AM
  4. Displaying array values in jstl page
    By keshaba in forum Advanced Java
    Replies: 0
    Last Post: 04-23-2010, 10:00 AM
  5. Problems passing a scan object to a method
    By xkross in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 03:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •