Spring MVC Controller working but not creating the specified response URL ,It is creating the url from request mapping string

前端 未结 2 1447
说谎
说谎 2021-01-28 16:22

I am studying spring MVC based webapps. So I created a project in spring mvc and I have chosen eclipse IDE.Apache tomcat 8 server and jre1.8 the version of spring package is 4.2

相关标签:
2条回答
  • 2021-01-28 17:00

    I changed the return type of method submitBranchInsert as String as follows

     @RequestMapping(value="/branchsubmitinsert.travel", method=RequestMethod.POST)
    public String submitBranchInsert(ModelMap model,@ModelAttribute BranchModel branchModel){
    
        //ModelAndView modelAndView = new ModelAndView("/branch/branchinsert");
        //modelAndView.addObject("branch",branchModel);
        //return modelAndView;
        model.addAttribute("branch", branchModel);
        return "/branch/branchinsert";
    
    }
    

    now its ok but why i can't use ModelAndView here instead of Model in String , may be my sound dumb to the experts in Spring MVC. I just want to know when should use ModelAndView instead of Model in String? I don't want to ask this question as separately that is why I am asking this with my answer itself. anybody can help me?

    0 讨论(0)
  • 2021-01-28 17:05

    Since you mentioned modules folder in the prefix of viewResolver it is working like that..

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/modules/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    

    Which means return "branch/branchInsert" will find jsp in /modules/branch/branchInsert.jsp.

    If you use just string to return means it's just an path from the jsp where you are sending the requset.So for that viewResolver wont add prefix folder.

    But if you use ModelAndView the viewResolver will add the prefix and suffix.

    0 讨论(0)
提交回复
热议问题