Results 1 to 7 of 7

Thread: HELP - weird thing happening with spring mvc

  1. #1
    Join Date
    May 2011
    Posts
    8

    Exclamation 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. #2
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

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

  3. #3
    Join Date
    Apr 2011
    Posts
    13

    Default

    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.

  4. #4
    Join Date
    May 2011
    Posts
    8

    Unhappy

    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;
        }

  5. #5
    Join Date
    Mar 2011
    Location
    Washington, DC
    Posts
    60

    Default

    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>
        }
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

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

  7. #7
    Join Date
    May 2011
    Posts
    8

    Wink 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!

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
  •