springmvc返回的路径

Posted on 2016-07-19 16:15  上善其若水,厚德载物  阅读(384)  评论(0)    收藏  举报

返回的字符串就是视图名称,如返回modelautobind就是最后页面要到达/WEB-INF/views/modelautobind.jsp

  <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
       <property name="contentType" value="text/html"/>        
       <property name="prefix" value="/WEB-INF/views/"/>
       <property name="suffix" value=".jsp"/>
    </bean>

 

 

@RequestMapping(value="/modelautobind", method = {RequestMethod.GET})
public String modelAutoBind(HttpServletRequest request, Model model){
    
    model.addAttribute("accountmodel", new AccountModel());
    return "modelautobind";
}

@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})
public String modelAutoBind(HttpServletRequest request, Model model, AccountModel accountModel){
    
    model.addAttribute("accountmodel", accountModel);
    return "modelautobindresult";
}

 

在刚才添加的2个action中可以看到返回类型和以前的不一样了由ModelAndView变成了String,这是由于Spring MVC 提供Model、ModelMap、Map让我们可以直接添加渲染视图需要的模型数据,在返回时直接指定对应视图名称就可以了。

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3