Announcement Announcement Module
Collapse
No announcement yet.
why jsp not show integer value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • why jsp not show integer value

    my domain object:
    Code:
    public class UserView {
    	private int id;
    	private String firstName = "test this";
    	private int disabledPermanently = 12;
    // getter and setter
    my controller:
    Code:
    public class UsersController extends MultiActionController {
    ...
    	// this method call when jsp is show
    	public ModelAndView create(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		ModelAndView mav = new ModelAndView("user", "commandName",
    				new UserView());
    		return mav;
    	}
    my jsp:
    HTML Code:
    <form:form method="POST" action="add.htm" commandName="commandName">
    				<tr>
    					<td><spring:message code="firstName" />*</td>
    					<td><form:input path="firstName" size="52" maxlength="100"
    						readonly="${isReadOnly}" /></td>
    				</tr>
    ${disabledPermanently}
    </form:form>
    as result in my jsp in the field firstName the default value is test this, but the value of disabledPermanently is empty. Why it is not 12?

  • #2
    I think it has to do with the form taglib knowing how to traverse the commandName object and find child properties by name.

    Hence, if you were to use <form:input path="${disabledPermanently}" /> you should see a text box with the value 12.

    Try referencing the model object itself and see if it finds the property. Change your "${disabledPermanently}" to "${commandName.disabledPermanently}".

    Comment


    • #3
      Originally posted by jamestastic View Post
      I think it has to do with the form taglib knowing how to traverse the commandName object and find child properties by name.

      Hence, if you were to use <form:input path="${disabledPermanently}" /> you should see a text box with the value 12.

      Try referencing the model object itself and see if it finds the property. Change your "${disabledPermanently}" to "${commandName.disabledPermanently}".
      Yesssss! It's work with "${commandName.disabledPermanently}"

      P.S. Let me buy you a beer after work!

      Comment


      • #4
        Glad to hear it. :]

        The reason this works is because you have an instance of the UserView class that you have added to the model. The instance on the model is called "commandName". Thus, to refer to properties of the UserView object, you use ${commandName.id}, ${commandName.firstName}, or ${commandName.disabledPermanently}.

        If you changed your code from:

        Code:
        ModelAndView mav = new ModelAndView("user", "commandName", new UserView());
        to the following:

        Code:
        ModelAndView mav = new ModelAndView("user", "userView", new UserView());
        then you could refer to the properties of the UserView object by the more appropriately named ${userView.id}, ${userView.firstName}, or ${userView.disabledPermanently}. Note that you would also need to change:

        Code:
        commandName="commandName"
        to:

        Code:
        commandName="userView"
        to reference the appropriate model attribute.

        See The form tag and Web MVC framework for more.

        Comment


        • #5
          When you use the form:form, you don't need to write the command name in every form tag (e.g: <form:input>, <form: option>, etc.), because you set it in the <form:form> tag previously (in <form:form method="POST" action="add.htm" commandName="commandName">) and it will set in every form tag automatically. But when you use a non-form tag, you have to write it, e.g: <c:if test="${commandName.firstName}">, <c: out value="${commandName.disabledPermanently}"/> or ${commandName.id}.

          Comment

          Working...
          X