SpringMVC注解开发:
实现一个Handler处理多个请求
@ Controller
@ RequestMapping(value="url",method=RequestMethod.GET/POST)
@ RequestParam : 当提交请求参数名和方法形参不一致时候使用
注解开发后 方法的返回值:
a.ModelAndView
需要在方法中定义ModelAndView.
往对象中添加模型数据和视图.
b.String
b.1 返回页面 return "addSuccess"; 跳转到jsp
b.2 重定向 return "redirect:list.action";
b.3 转发 return "forward:/list.action";
c.void 空返回
方法的参数绑定:
a.基本类型+String eg: list(String name);
b.对象类型 eg: add(User u);c.request,response,session,Model(底层就是Map)
SpringMVC注解编程
handle类
@Controller //标识控制器
public class UserHandler {
String account;
//@RequestMapping: 标识url 方法映射
@RequestMapping(value="list.action",method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView list(String name,String sex){
ModelAndView mav = new ModelAndView();
User a;
List<Users> list = new ArrayList<Users>();
list.add(new Users(1,"admin","admin@163.com","22"));
list.add(new Users(2,"common","common@163.com","21"));
mav.addObject("list", list);
mav.addObject("n", name);
mav.addObject("s", sex);
mav.setViewName("list");
return mav;
}
//用对象类型接受页面的多个值 封装成一个对象
//可以在自定义的方法 参数绑定:基本类型 String,对象,request,response,session,Model(底层就是一个Map结构)//可以在自定义的方法 参数绑定:基本类型 String,对象,request,response,session,Model(底层就是一个Map结构)
//可以在自定义的方法 参数绑定:基本类型 String,对象,request,response,session,Model(底层就是一个Map结构)
@RequestMapping(value="add.action",method={RequestMethod.GET})
public Stringadd(Users u,HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model) throws IOException{
System.out.println(u.getUsername()+":"+u.getAge());
model.addAttribute("aaa", "啊啊啊啊");
return "addSuccess";//跳转到页面
// return "redirect:list.action";
// return "forward:/list.action";
}
@RequestMapping(value="delete.action",method={RequestMethod.GET,RequestMethod.POST})
public void delete(String username,String email,String age,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
Users u = new Users(username,email,age);
request.setAttribute("u", u);
//重定向到 完整页面
//response.sendRedirect("/jsp/deleteSuccess.jsp");
request.getRequestDispatcher("/jsp/deleteSuccess.jsp").forward(request, response);
}
}
配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 启用注解扫描 -->
<context:component-scan base-package="com.controller">
</context:component-scan>
<!-- 映射器
只能在注解开发的Handler使用此种映射器
-->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.class">
</bean> -->
<!-- 适配器
只能在注解开发的Handler 使用
-->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean> -->
<!-- 映射器和适配器在Dispatcher.propertice里已经提供,所有不用配置-->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
配置web.xml
<!-- 配置springmvc的前端控制器 -->
<servlet>
<servlet-name>springmvc1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc1</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping