步一:创建springmvc-day02这么一个web应用
步二:导入springioc,springweb和springmvc相关的jar包
------------------------------------------------------springWEB模块 org.springframework.web-3.0.5.RELEASE.jar org.springframework.web.servlet-3.0.5.RELEASE.jar(mvc专用) ------------------------------------------------------springIOC模块 org.springframework.asm-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE.jar org.springframework.context-3.0.5.RELEASE.jar org.springframework.core-3.0.5.RELEASE.jar org.springframework.expression-3.0.5.RELEASE.jar步三:在/WEB-INF/下创建web.xml文件
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>步四:创建HelloAction.java控制器类
@Controller public class HelloAction{ @RequestMapping(value="/hello") public String helloMethod(Model model) throws Exception{ System.out.println("HelloAction::helloMethod()"); model.addAttribute("message","这是我的第二个springmvc应用程序"); return "/success.jsp"; } }步五:在/WebRoot/下创建success.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>这是我的第二个springmvc应用程序</title> </head> <body> success.jsp<br/> ${message} </body> </html>步六:在/src/目录下创建spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- Action控制器 --> <context:component-scan base-package="com.zc.javaee.springmvc.helloannotation"/> <!-- 基于注解的映射器(可选) --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 基于注解的适配器(可选) --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 视图解析器(可选) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>springmvc可以在业务控制方法中,以参数形式收集客户端参数
@Controller @RequestMapping(value="/user") public class UserAction{ @RequestMapping(value="/add") public String add(Model model,int id,String name,Double sal) throws Exception{ System.out.println("HelloAction::add()"); System.out.println(id + ":" + name + ":" + sal); model.addAttribute("message","增加用户"); return "/success.jsp"; } }可以在业务控制方法中书写1个模型来收集客户端的参数,模型中的属性名必须和客户端参数名一一对应
@Controller @RequestMapping(value = "/user") public class UserAction { @InitBinder protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(User user,Model model) throws Exception { System.out.println("HelloAction::add()::POST"); model.addAttribute("user",user); return "/register.jsp"; } }
可以在业务控制方法中书写0个或多个模型来收集客户端的参数,当多个模型中有相同的属性时,可以用user.name或admin.name来收集客户端参数,解决方法是用一个新的模型将User和Admin再封装一次
jsp页面
<form action="${pageContext.request.contextPath}/person/add.action" method="POST"> 编号:<input type="text" name="user.id" value="${bean.user.id}"/><br/> 姓名:<input type="text" name="user.name" value="${bean.user.name}"/><br/> 薪水:<input type="text" name="user.sal" value="${bean.user.sal}"/><br/> 入职时间:<input type="text" name="user.hiredate" value='<fmt:formatDate value="${bean.user.hiredate}" type="both" />'/><br/> <input type="submit" value="注册"/> </form> @Controller @RequestMapping(value = "/person") public class PersonAction { @InitBinder protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(Bean bean,Model model) throws Exception { System.out.println(bean.getUser()); System.out.println(bean.getAdmin()); System.out.println("PersonAction::add()::POST"); model.addAttribute("bean",bean); return "/register.jsp"; } }提交表单后,将JavaBean信息以JSON文本形式返回到浏览器
//bean2json.jsp <form> 编号:<input type="text" name="id" value="1"/><br/> 姓名:<input type="text" name="name" value="哈哈"/><br/> 薪水:<input type="text" name="sal" value="5000"/><br/> <input type="button" value="异步提交注册"/> </form> <script type="text/javascript"> $(":button").click(function(){ var url = "${pageContext.request.contextPath}/user/add.action"; var sendData = { "id":1, "name":"哈哈", "sal":5000 }; $.post(url,sendData,function(backData,textStatus,ajax){ alert(ajax.responseText); }); }); </script> //UserAction.java @Controller @RequestMapping(value="/user") public class UserAction { @RequestMapping(value="/add") public @ResponseBody User add(User user) throws Exception{ System.out.println(user.getId()+":"+user.getName()+":"+user.getSal()); return user; } } //spring.xml <!-- Action控制器 --> <context:component-scan base-package="com.zc.javaee.springmvc.app25"/> <!-- 适配器 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean>