您的位置:首页 > 编程语言 > Java开发

springmvc之rest风格的增删改查

2015-09-16 23:27 429 查看

全部代码下载地址:http://download.csdn.net/detail/u012309366/9112935

jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

my name is guoxp!

<!-- Rest CRUD -->

<form action="postTest" method="post">

<input type="submit" value="post"/>

</form>

<form action="deleteTest/1" method="post">

<input type="hidden" name="_method" value="DELETE"/>

<input type="submit" value="delete"/>

</form>

<form action="updateTest/1" method="post">

<input type="hidden" name="_method" value="PUT"></input>

<input type="submit" value="update"></input>

</form>

<a href="getTest/1">get</a>

</body>

</html>

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_2_5.xsd"
id="WebApp_ID" version="2.5">

<!-- 配置hiddenhttpMethodFilter:可以将post请求转化成put、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>

<!-- 配置 DispatcherServlet -->

<servlet>

<servlet-name>dispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->

<!--

实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.

默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml

-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

springmvc.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 配置自定扫描的包 -->

<context:component-scan base-package="com.gxp.cc"></context:component-scan>

<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

java文件

package com.gxp.cc;

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.RequestMethod;

@Controller

public class RestCRUD {

@RequestMapping(value="/postTest",method=RequestMethod.POST)

public String insert(){

System.out.println("insert");

return "success";

}

@RequestMapping(value="/deleteTest/{id}",method=RequestMethod.DELETE)

public String delete(@PathVariable Integer id){

System.out.println("DELETE"+id);

return "success";

}

@RequestMapping(value="/updateTest/{id}",method=RequestMethod.PUT)

public String update(@PathVariable Integer id){

System.out.println("UPDATE"+id);

return "success";

}

@RequestMapping(value="/getTest/{id}",method=RequestMethod.GET)

public String get(@PathVariable Integer id){

System.out.println("GET"+id);

return "success";

}

}

响应页面jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

success page!

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: