SpringMVC学习笔记(十三)

来源:互联网 发布:万网域名备案在哪 编辑:程序博客网 时间:2024/05/22 03:50

1、SpringMVC RESTful支持

理解什么叫做RESTful架构。

2、简单实例

(1)项目结构


(2)Item.java

package cn.hwd.springmvc.bean;public class Item {private int id;private String name;private double price;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public Item() {super();// TODO Auto-generated constructor stub}public Item(int id, String name, double price) {super();this.id = id;this.name = name;this.price = price;}@Overridepublic String toString() {return "Item [id=" + id + ", name=" + name + ", price=" + price + "]";}}

(3)HelloController.java

package cn.hwd.springmvc.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import cn.hwd.springmvc.bean.Item;@Controller@RequestMapping("/hello")public class HelloController {/** * /index/{id}/{name}/{price}中的{id}表示将这个位置的参数传送到@PathVariable("id")指定的变量之中,{name}和{price}类似 * @param id * @param name * @param price * @return * @throws Exception */@RequestMapping("/index/{id}/{name}/{price}")      public @ResponseBody Item index(@PathVariable("id") Integer id,     @PathVariable("name") String name, @PathVariable("price") double price) throws Exception {        return new Item(id, name, price);      }}

(4)spring-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-4.3.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.3.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">        <context:component-scan base-package="cn.hwd.springmvc.controller" />    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/page/" /><property name="suffix" value=".jsp" /></bean><!-- 配置json转换器 --><mvc:annotation-driven /><!-- 静态资源解析 --><mvc:resources location="/js/" mapping="/js/**" /></beans>

(5)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>cn.hwd</groupId>  <artifactId>SpringMVC41</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>SpringMVC41 Maven Webapp</name>  <url>http://maven.apache.org</url>  <build>    <finalName>SpringMVC41</finalName>    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-compiler-plugin</artifactId>        <version>3.0</version>        <configuration>          <source>1.7</source>          <target>1.7</target>        </configuration>      </plugin>    </plugins>  </build>  <dependencies>  <dependency>  <groupId>javax.servlet</groupId>  <artifactId>servlet-api</artifactId>  <version>2.5</version>  </dependency>  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  <version>4.0.2.RELEASE</version></dependency><!-- json jar包 --><dependency>    <groupId>org.codehaus.jackson</groupId>    <artifactId>jackson-core-asl</artifactId>    <version>1.9.11</version></dependency><dependency>    <groupId>org.codehaus.jackson</groupId>    <artifactId>jackson-mapper-asl</artifactId>    <version>1.9.11</version></dependency>  </dependencies></project>

(6)web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>Archetype Created Web Application</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <servlet>    <servlet-name>spring</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring-servlet.xml</param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping></web-app>

(7)运行结果



原创粉丝点击