Announcement Announcement Module
No announcement yet.
NewBie - how to set command object Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • NewBie - how to set command object


    I am new to spring mvc. I am stuck at one place.

    I have a jsp page haviong a menu which is having a list of hyperlinks.

    <form action="" method="post">
         <a href=# id="href_12" title="Parent Link" class="header closed_red">Parent Link</a>
         <div id="div_2" class="closed_container">
                   <a href=""> Child Link 1 </a>
    	   <a href="" >Child Link 2</a>
    	   <a href="" >Child Link 3</a>
    My requirement is - on click of any child link i have to go to the AbstractCommandController .
    That i guess i can do with
    <form action='<c:url value="/processDetails.html"/>' method="post">
    i also want to set the command object , so that on the basis of the command object i can return the view name from the controller. The command object i feel should be string and the value set for comand object will be name of the link.

    How can is set the command object value from jsp.

    I have seen the spring:bing tags, but the examples i have seen has input as tetbox or checkbox.
    Can anybody help me how can is set for hypelinks ?
    And also where i can get good tutorials related to spring tags.
    This is something urgent i want .

  • #2
    HTML forms aren't really designed to work this way... You could mess around with JavaScript and hidden input fields... Why use a form at all?

    Why not just use the links to direct to the correct view? Are these links being used for navigation on the site, or are they being used as part of a user input form? If they're just for navigation, don't bother with the form.


    • #3
      Thanks for the reply,

      Originally posted by mbeards View Post

      Why not just use the links to direct to the correct view?
      My application has 30-40 menus(links). For redirecting directly to the crrect view, i have to create same number of controllers which i dont want to do.
      My idea is that i have one controller and on the basis of command object i return the appropriate view when the menu is clicked.

      Is there some way out for it ?


      • #4
        For non-form pages (that is, pages that don't need to gather and process user input), I just use a single MultiActionController.

        For example, if you have something like:

        <a href="home.html">Home</a>
        <a href="aboutus.html">About Us</a>
        <a href="services.html">Services</a>
        <a href="products.html">Products</a>
        <a href="directory.html">Employee Directory</a>

        Then, in your Spring servlet config:
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="mappings">
              <prop key="home.html">staticPageController</prop>
              <prop key="aboutus.html">staticPageController</prop>
              <prop key="services.html">staticPageController</prop>
        <bean id="staticPageController" class="com.example.StaticPageController">
          <!-- maybe inject some property here like a business service object -->
        Then, your controller should extend MultiActionController and you define as many public ModelAndView() methods as you need.

        public ModelAndView home(HttpServletRequest request, HttpServletResponse response) throws Exception)
          return new ModelAndView("home");
        public ModelAndView aboutus(...
        public ModelAndView services(...
        public ModelAndView products(...
        You can also use other URL mapping strategies besides SimpleUrlHandlerMapping. The ResourceBundleViewResolver offers good flexibility and localization. In that case, you define your views in a separate properties file. You can use wildcards too to decrease the amount of XML you have to write.