欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring3实现MVC的rest

程序员文章站 2022-07-08 10:24:28
...

首先配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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">



	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!--
		浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http
		delete方法
	-->
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<servlet-name>springmvc</servlet-name>
	</filter-mapping>

</web-app>

 

 

然后配置springmvc-servlet.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>

<!-- 
	修改说明:
	1.修改<context:component-scan以确定要加载的controller,默认为com.**.controller
	2.修改class=ControllerClassNameHandlerMapping节点的pathPrefix以确定要生成的前缀
 -->
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName"   >
    <!-- 对web包中的所有controller类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.**.controller" />
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->		
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
	 p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/>

        
</beans>

 

 

 

SERVLET类:

 

 

package com.insigma.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.insigma.domain.Person;

@Controller
@RequestMapping("/people")
public class UserInfoController {

	private static List list = new ArrayList<Person>();

	static {
		Person person = new Person();
		person.setId(new Long(1));
		person.setName("He Jing");
		person.setTitle("None");
		list.add(person);
		Person person1 = new Person();
		person1.setId(new Long(2));
		person1.setName("Kevin");
		person1.setTitle("Who");
		list.add(person1);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String getAll(Model model) {
		System.out.println("=============> Get list");
		model.addAttribute("list", list);
		return "people";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getPerson(@PathVariable("id") Long personId, Model model) {
		System.out.println("=========> Get one person");
		Person person;
		List list2 = new ArrayList<Person>();
		for (int i = 0; i < list.size(); i++) {
			person = (Person) list.get(i);
			if (person.getId().equals(personId)) {
				list2.add(person);
			}
		}
		model.addAttribute("list", list2);
		return "people";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String savePerson(Person person, Model model) {
		list.add(person);
		model.addAttribute("list", list);
		return "people";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String deletePerson(@PathVariable("id") Long personId, Model model) {
		Person person;
		for (int i = 0; i < list.size(); i++) {
			person = (Person) list.get(i);
			if (person.getId().equals(personId)) {
				list.remove(i);
			}
		}
		model.addAttribute("list", list);
		return "people";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public String updatePerson(@PathVariable("id") Long personId, Person person, Model model) {
		Person person2;
		for (int i = 0; i < list.size(); i++) {
			person2 = (Person) list.get(i);
			if (person2.getId().equals(personId)) {
				person2.setId(person.getId());
				person2.setName(person.getName());
				person2.setTitle(person.getTitle());
			}
		}
		model.addAttribute("list", list);
		return "people";
	}
}

 

 

 

people.jsp:

 

 

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<html>
<body>
<table>
	<tr>
		<td>People id</td>
		<td>People name</td>
		<td>People title</td>
		<td></td>
	</tr>
	<c:forEach items="${list}" var="people">
		<tr>
			<td><input id="pid${people.id}" type="text" value='${people.id}'/></td>
			<td><input id="pna${people.id}" type="text" value='${people.name}'/></td>
			<td><input id="pti${people.id}" type="text" value='${people.title}'/></td>
			<td><input type="button" value="Update" onClick="upd('${people.id}')" /></td>
			<td><input type="button" value="Delete" onClick="del('${people.id}')" /></td>
		</tr>
	</c:forEach>



	<form id="upForm" action="/TestREST/people" method="POST">
		<input id="people_id" name="id" type="hidden" />
		<input id="people_name" name="name" type="hidden" />
		<input id="people_title" name="title" type="hidden" />
		<input type="hidden" name="_method" value="put" />
	</form>

	<form id="delForm" action="/TestREST/people/" method="POST"><input
		type="hidden" name="_method" value="delete" /></form>
	</table>


	<table>
		<form action="/TestREST/people" method="POST">
			<tr>
				<td>people_id:<input id="people_id" name="id" type="text" /></td>
			</tr>
			<tr>
				<td>people_name:<input id="people_name" name="name" type="text" /></td>
			</tr>
			<tr>
				<td>people_title:<input id="people_title" name="title" type="text" /></td>
			</tr>
			<tr>
				<td><input type="submit" value="Add" /></td>
			</tr>		
		</form>
	</table>

</body>
<Script>
	function del(id) {
		document.getElementById('delForm').action = '/TestREST/people/' + id;
		document.getElementById('delForm').submit();
	}

	function upd(id) {
		var form=document.getElementById('upForm');
		form.action = '/TestREST/people/' + id;
		form.people_id.value=document.getElementById('pid'+id).value;
		form.people_name.value=document.getElementById('pna'+id).value;
		form.people_title.value=document.getElementById('pti'+id).value;
		form.submit();
	}
</Script>
</html>

 

 

 

上一篇: c#基础(四)

下一篇: 什么是MVC