常用的映射处理器配置与常用Controller的方法
<!--可以显示的定义HandleMapping,以bean的name来做映射处理器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--简单的HanndleMapping映射处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">controllerId</prop>
</props>
</property>
</bean>
<bean class="org.com.controller.ControllerDemo" id="controllerId" ></bean>
<!--控制器的类名映射处理器,注意访问时类名首字母小写.do-->
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping">
</bean>
<!--接收参数控制器,这里可以接收一个对象 -->
<bean id="commController" name="/comm.do" class="org.com.controller.ControllerDemo">
<property name="commanClass" value="org.com.controller.Person"></property>
</bean>
<!--表单控制器-->
<bean id="formCointroller" class="org.com.controller.ControllerDemo" name="/controllerDemo2.do">
<property name="commanClass" value="org.com.controller.Person"></property>
<property name="formView" value=""></property>
<property name="successView" value=""></property>
</bean>
@InitBinder
public void binder(ServletRequestDataBinder binder) {
//绑定时间类型的属性编辑器
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
Controoler方法
/**
*接收一个Person对象,前端页面传递过来的参数,对应Person的属性
*
*/
@RequestMapping(value = "/toPerson1",method = RequestMethod.POST)
public String toPerson1(Person person) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("person",map);
return "index";
}
/**
* 数据和视图一块返回
* @param person
* @return
*/
@RequestMapping(value = "/toPerson3",method = RequestMethod.POST)
public ModelAndView toPerson3(Person person) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("person",person);
//相当于request.setAttribute("person",person);
//return "index";
return new ModelAndView("index",map);
}
/**
* 数据和视图是分离的,这样写最好
* @param person
* @param model
* @return
*/
@RequestMapping(value = "/toPerson4", method = RequestMethod.POST)
public String toPerson4(Person person,Model model) {
model.addAttribute("person",person);
//相当于request.setAttribute("person",person);
return "index";
}
/**
* 在参数中直接定义map,可以将数据封装成model
* @param person
* @param map
* @return
*/
@RequestMapping(value = "/toPerson5",method = RequestMethod.POST)
public String toPerson5(Person person,Map<String,Object> map) {
map.put("person",person);
return "index";
}
/**
* 在参数列表中定义HttpServletResponse,用来返回ajax的结果
* @param person
* @param response
*/
@RequestMapping(value = "/toAjax.do",method = RequestMethod.POST)
public void toAjax(Person person, HttpServletResponse response) {
try {
response.getWriter().write(person.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 直接在参数列表定义PrintWriter返回ajax的结果,建议使用
* @param person
* @param out
*/
@RequestMapping(value = "/toAjax1.do",method = RequestMethod.POST)
public void toAjax1(Person person, PrintWriter out) {
out.write(person.getName());
}
/**
* 内部重定向,也叫做客户端重定向,浏览器的地址栏会发生变化,不需要namespace命名空间
* @return
*/
@RequestMapping(value = "/redirectAjax.do")
public String redirectAjax() {
return "redirect:ajax.do";
}
/**
* Controller之间的重定向,必须加上命名空间,也必须加上/表示退回到项目的根路径,如果没有加上的话,则会自动追加
* @return
*/
@RequestMapping(value = "redirectAjax1.do")
public String redirectAjax1() {
return "redirect:/text1/text1.do";
}