开发者

Spring MVC - Basic RequestMapping question

I'm new to Spring MVC. I'm getting errors on the following (not sure yet what;s the full scope of info requierd to assist me):

Working fine:

 @RequestMapping(value = "startpage.do")
 public ModelAndView startpage(HttpServletRequest req, HttpServletResponse res) {
.
.
  ModelAndView mv = new ModelAndView("startpage");
  mv.getModelMap().addAttribute("loginPage", loginPage);
  return mv;

But failing:

         @RequestMapping(value = "somecontroller.do")
 public ModelAndView ftcontroller(HttpServletRequest req, HttpServletResponse res, ModelAndView mav) {.. ...
   ModelAndView mv = new ModelAndView("startpage");
      mv.getModelMap().addAttribute("loginPage", loginPage);
  return mav;

As you can see, same code, different request mapping. Could it be that this is consuing the MVC somehow to get confused?

The error I'm getting is:

  java.lang.NullPointerException
 at jsp_servlet._web_45_inf._jsp.__somecontroller._jspService(__ftcontroller.java:103)
 at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
 at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
 at weblogic.servlet.internal.StubSecurityHelper.invokeServ开发者_运维百科let(StubSecurityHelper.java:125)
 at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
 Truncated. see log file for complete stacktrace

The biz logic is a login screen "startpage.do" when Login is submitted, it can fail (in which case, startup.do should be retuend once again, in other case, a differn page should be returned.

if there is better way to implement this, I'll be happy to hear that.

(It should be noted that I'm trying to plug-in Spring MVC into an existing project, so I'm trying to make as little as possible changes, and hence not using spring:form etc..)

More info:

  1. The JSP startpage.jsp (resolved from "startpage") has a form with target="somecontroller.do".

  2. Full controller code:

    class

    { public static void main(String[] args) {

    @RequestMapping(value = "startpage.do")
    public ModelAndView startpage(HttpServletRequest req, HttpServletResponse res) {
        System.out.println(">>>>>>HomeController: Passing through (Get Type)...");
        LoginPage loginPage = new LoginPage();
        ModelAndView mv = new ModelAndView("startpage");
        mv.getModelMap().addAttribute("loginPage", loginPage);
        return mv;
    }
    
    @RequestMapping(value = "somecontroller.do")
    public ModelAndView ftcontroller(HttpServletRequest req, HttpServletResponse res)
            throws Exception {
    
        // Parsing for login request;
        String sUsername = req.getParameter(USER_ID);
        String sUserPassword = req.getParameter(PASSWORD);
        AbstractResponseDataComponent returnedResponse = new LoginCommand().login(sUsername, sUserPassword);
    
        String returnedView = GlobalConstants.EMPTY_STRING;
        JstlView view = new JstlView();
        Map model = new HashMap();
        if (returnedResponse.isSuccessful()) 
        {
            view.setUrl("somecontroller");
            model.put("loginResponse", (LoginResponse) returnedResponse);
    
        } else 
        {
            view.setUrl("startpage");
            model.put("loginPage", (LoginPage) returnedResponse);
        }
        return new ModelAndView(view, model);
    
    }
    }
    

    }


BTW: when using Spring 3.0 make your method signature more clean:

instead of

public ModelAndView ftcontroller(HttpServletRequest req, HttpServletResponse res)
    throws Exception {
  String sUsername = req.getParameter(USER_ID);
  String sUserPassword = req.getParameter(PASSWORD);
...

do it in the spring 3.0 way:

public ModelAndView ftcontroller(
   @RequestParam(USER_ID) String sUsername, 
   @RequestParam(PASSWORD) String sUserPassword)
    throws Exception {

...


For your second question:

if there is better way to implement this, I'll be happy to hear that.

Are you looking for something like this:

@RequestMapping(value = "startpage.do")
public String startpage() {
    ...    
    if (loginFailed) {
       return "redirect:startpage.do");
    } else {
       return "redirect:somecontroller.do");
    }
}

(I prefere redirects, because I assume the that login methods has some sideeffects.)


I belive the cause of your exception is the JstlView.

Try not to use the JstlView direct, instad pass the view name as String.

final String viewName;
Map model = new HashMap();
if (returnedResponse.isSuccessful()) 
{
    viewName = "somecontroller";
    model.put("loginResponse", (LoginResponse) returnedResponse);

} else 
{
    viewName = "startpage";
    model.put("loginPage", (LoginPage) returnedResponse);
}
return new ModelAndView(viewName, model);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜