Spring MVC+Spring3+Hibernate4开发环境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm Spring MVC整合Freemarker基于注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm 基于注解的Spring MVC简单介绍 http://www.linuxidc.com/Linux/2012-02/54896.htm Spring MVC 框架搭建及详解 http://www.linuxidc.com/Linux/2012-01/52740.htm
一、SpringMVC基础入门,创建一个HelloWorld程序
1.首先,导入SpringMVC需要的jar包。2.添加Web.xml配置文件中关于SpringMVC的配置 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc-servlet.xml</param-value></init-param><!-- <load-on-startup>1</load-on-startup> --></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping> 3.在src下添加springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!-- scan the package and the sub package --><context:component-scan base-package="test.SpringMVC"/><!-- don"t handle the static resource --><mvc:default-servlet-handler /><!-- if you use annotation you must configure following setting --><mvc:annotation-driven /><!-- configure the InternalResourceViewResolver --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 后缀 --><property name="suffix" value=".jsp" /></bean></beans> 4.在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp视图。创建一个hello.jsp,在body中添加“Hello World”。5.建立包及Controller,如下所示6.编写Controller代码@Controller@RequestMapping("/mvc")public class mvcController {@RequestMapping("/hello")public String hello(){return "hello";}} 7.启动服务器,键入 http://localhost:8080/项目名/mvc/hello
1.编写一个Person实体类 package test.SpringMVC.model;public class Person {public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}private String name;private int age;} 2.在Controller里编写方法 //boxing automatically@RequestMapping("/person1")public String toPerson(Person p){System.out.println(p.getName()+" "+p.getAge());return "hello";}
六、使用InitBinder来处理Date类型的参数
//the parameter was converted in initBinder@RequestMapping("/date")public String date(Date date){System.out.println(date);return "hello";}//At the time of initialization,convert the type "String" to type "date"@InitBinderpublic void initBinder(ServletRequestDataBinder binder){binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));}
七、向前台传递参数
//pass the parameters to front-end@RequestMapping("/show")public String showPerson(Map<String,Object> map){Person p =new Person();map.put("p", p);p.setAge(20);p.setName("jayjay");return "show";} 前台可在Request域中取到"p"
八、使用Ajax调用
//pass the parameters to front-end using ajax@RequestMapping("/getPerson")public void getPerson(String name,PrintWriter pw){pw.write("hello,"+name);}@RequestMapping("/name")public String sayHello(){return "name";} 前台用下面的Jquery代码调用 $(function(){$("#btn").click(function(){$.post("mvc/getPerson",{name:$("#name").val()},function(data){alert(data);});});});
1.RestController @Controller@RequestMapping("/rest")public class RestController {@RequestMapping(value="/user/{id}",method=RequestMethod.GET)public String get(@PathVariable("id") Integer id){System.out.println("get"+id);return "/hello";}@RequestMapping(value="/user/{id}",method=RequestMethod.POST)public String post(@PathVariable("id") Integer id){System.out.println("post"+id);return "/hello";}@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)public String put(@PathVariable("id") Integer id){System.out.println("put"+id);return "/hello";}@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)public String delete(@PathVariable("id") Integer id){System.out.println("delete"+id);return "/hello";}} 2.form表单发送put和delete请求在web.xml中配置 <!-- configure the HiddenHttpMethodFilter,convert the post method to put or delete --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> 在前台可以用以下代码产生请求 <form action="rest/user/1" method="post"><input type="hidden" name="_method" value="PUT"><input type="submit" value="put"></form><form action="rest/user/1" method="post"><input type="submit" value="post"></form><form action="rest/user/1" method="get"><input type="submit" value="get"></form><form action="rest/user/1" method="post"><input type="hidden" name="_method" value="DELETE"><input type="submit" value="delete"></form>
十三、返回json格式的字符串
1.导入以下jar包2.方法代码 @Controller@RequestMapping("/json")public class jsonController {@ResponseBody@RequestMapping("/user")publicUser get(){User u = new User();u.setId(1);u.setName("jayjay");u.setBirth(new Date());return u;}}