Announcement Announcement Module
Collapse
No announcement yet.
HELP - weird thing happening with spring mvc Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HELP - weird thing happening with spring mvc

    Need help, this made my head spin. Look at the code

    This does not work
    Code:
    @Controller
    public class EmployeeProfileController {
    
        @RequestMapping("/expenses_employee")
        public Object index(HttpSession session) {
            ModelAndView mv = new ModelAndView("home");
            
            return mv;
        }
    }
    However, this does

    Code:
    @Controller
    public class EmployeeProfileController {
    
        @RequestMapping("/expenses_employee")
        public Object index(HttpSession session) {
            
            return "home";
        }
    }
    I used this as resolver
    Code:
    <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
            <property name="order" value="1" />
        </bean>
    The weird thing is that 2 of my controllers having EXACTLY the same code, works for both cases.
    The problem is that I need to pass variables to the View from the Controller.

    Note that this is only a sample code, if the code logic did not make sense just ignore it. I just want to know why it does not work.

    I am becoming desperate here
    Please I need help.
    Thank you for any help you can provide.

  • #2
    Change your return types to match more closely what you're returning. Change "Object" to "ModelAndView", and it should work better.

    Comment


    • #3
      Perhaps when the return type is Object they default to using toString(). The toString() for a ModelAndView does not return the view name.

      If you specified the return type, which you should, then it works fine.

      Comment


      • #4
        That was what I tried before I changed it to object.

        It does not care what viewname I use, it still uses the request mapping.
        Like in the code below, instead of going to home.jsp, it goes to expenses_employee.jsp and drops all objects I included in the ModelAndView

        Code:
        @RequestMapping("/expenses_employee")
            public ModelAndView index(HttpSession session) {
                ModelAndView mv = new ModelAndView("home");
                mv.addObject("pageIndex", Constants.EXPENSES_PAGE_INDEX);
                return mv;
            }

        Comment


        • #5
          Do this and see if this works for you

          Code:
          @Controller
          @RequestMapping("/expenses_employee")
          public class EmployeeProfileController {
          
               @RequestMapping(method=RequestMethod.GET)
              public String index(HttpSession session, ModelMap mv) {
                  mv.addObject("pageIndex", Constants.EXPENSES_PAGE_INDEX);
                  return <put yr url where you want it goes>
              }
          }

          Comment


          • #6
            What is the point of the HttpSession parameter? Could you please try removing that?

            Comment


            • #7
              Thanks a lot!

              WOW it works now. Thanks everyone for your help! This is the simplified code that still works employing the same code
              Code:
              @Controller
              public class EmployeePayrollController {
              
                  @RequestMapping("expenses_payroll")
                  public String index(HttpSession session, ModelMap mv) {
                      mv.addAttribute("pageIndex", Constants.EXPENSES_PAGE_INDEX);
                      return "expenses_payroll";
                  }
              }

              By the way, why did the old code not work? I would like to understand so that I could better comprehend Spring... By appearance, it should have worked, to me at least, and it did work on some controllers

              Thanks everyone for your help!

              Comment

              Working...
              X