springmvc.xml配置文件
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.me.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
参数类型和三种返回类型
参数类型
- HttpServletRequest
- HttpServletResponse
- Model
处理器示例1
@RequestMapping(value = "first",method = RequestMethod.GET)
public ModelAndView first(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("msg","这是一个springMVC注解开发");
modelAndView.setViewName("hello");
return modelAndView;
}
处理器示例2(返回类型)
@Controller
@RequestMapping("two")
public class TwoController {
@RequestMapping("/first")
public ModelAndView first(HttpServletRequest request, HttpServletResponse response){
ModelAndView modelAndView=new ModelAndView();
String name=request.getParameter("name");
request.setAttribute("name","request域对象:"+name);
modelAndView.setViewName("hello");
return modelAndView;
}
@RequestMapping("/second")
public void second(HttpServletRequest request, HttpServletResponse response){
try {
request.setAttribute("name","李四");
request.getRequestDispatcher("/one/first.action").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/third")
public void fourth(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setAttribute("name","kim");
response.sendRedirect(request.getContextPath()+"/one/first.action");
}
@RequestMapping("/fourth")
public String third(){
System.err.println("到达third");
return "forward:/one/first.action";
}
@RequestMapping("/fifth")
public String fifth(){
return "redirect:/one/first.action";
}
@RequestMapping("/sixth")
public String sisth(){
return "hello";
}
}