SpringMvc comment manner Handler

First, open springioc scan package

Then the class again for business processes annotated as Controller and then on the class to write their own business methods, and finally @RequestMapping ( "/ a url") written on the corresponding method on it, there are three methods of writing kind

(1) The return value is ModelAndView

(2) The return value is a string, the parameter value is the Model

(3) The return value is a string, the parameter value is a Map <String, String>

For chestnuts

@Controller
public class MyAnnotionHandler {
// annotation mode
    @RequestMapping ( "/ annotionTest")
  public ModelAndView FindName () {
    ModelAndView Music Videos new new ModelAndView = ();
    // add model data
    mv.addObject ( "name", "Tom ");
    // Add the logical view
    mv.setViewName ( "Show");
    return Music Videos;

  }

// 将Model和view分开
  @RequestMapping("/splitMV")
  public String splitModelAndView(Model model) {
    model.addAttribute("name", "Jerry");
    return "show";
  }

// map作为参数返回string
  @RequestMapping("/map")
  public String mapToModel(Map<String, String> map) {
    map.put("name", "Charis");
    return "show";
  }
}

Of course, show where you need to view parser configured, and then be able to access the /show.jsp. Then spring.xml view resolver configuration <bean> it

For chestnuts

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".jsp"></property>
</bean>

Guess you like

Origin www.cnblogs.com/liuyongbo/p/11038969.html