码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc-2(rest风格的增删改查)-代码(2)

时间:2017-12-05 17:47:53      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:success   lin   请求   w3c   version   XML   tags   1.0   spn   

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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  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">
<context:component-scan base-package="*"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--
     springmvc如何处理静态资源(所有的url认为是映射):
      1.<mvc:default-servlet-handler/>让核心控制器dispatcherServlet所有的映射失效
      2.<mvc:annotation-driven></mvc:annotation-driven>
 -->
 <mvc:default-servlet-handler/>
 <mvc:annotation-driven></mvc:annotation-driven>
</beans>

-------------------------------------------------------------------------------------------------------------------------------------------------------

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">
 <!-- The front controller of this Spring Web application, responsible for
  handling all application requests -->
 <servlet>
  <servlet-name>springDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- Map all requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>springDispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 <filter>
  <filter-name>HiddenMethod</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>HiddenMethod</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

--------------------------------------------------------------------------------------------------------------------

add.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
</head>
<body>
 <h1>${command.id!=0?‘UPDATE THE EMPLOYEE‘:‘ADD THE EMPLOYEE‘}</h1>
 <form:form method="post" action="../emp">
 <c:if test="${ command.id!=0}">
   <input type="hidden" name="_method" value="put"/>
   <form:hidden path="id"/>
 
 </c:if>
  <table>
   <tr>
    <td>NAME</td>
    <td><form:input path="name" /></td>
   </tr>
   <tr>
    <td>MAIL</td>
    <td><form:input path="mail" /></td>
   </tr>
   <c:if test="${command.id==0}">
   <tr>
    <td>GENDER</td>
    <td><form:radiobuttons path="gender"
      items="${requestScope.genders}" /></td>
   </tr>
   </c:if>
   <tr>
    <td>DEPARTMENT</td>
    <td><form:select path="dept.deptId"
      items="${requestScope.depts}" itemLabel="deptName"
      itemValue="deptId" /></td>
   </tr>
   <tr>
    <td><input type="submit"
     value="${command.id!=0?‘UPDATE‘:‘ADD‘ }" /></td>
   </tr>
  </table>

 


 </form:form>

</body>
</html>

--------------------------------------------------------------------------------------------------

index.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>Welcome to index</title>
</head>
<body>
<a href="showEmp">Show All Employees</a><br/>
<a href="addEmp/0">Add An Employee</a>
</body>
</html>

------------------------------------------------------------------------------------------------------

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>All Employees</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script>
 $(function() {
  $(‘a:first‘).click(function() {
   $(‘form‘).attr(‘action‘, $(‘a:first‘).attr(‘href‘)).submit();
   return false;
  });
 });
</script>
</head>
<body>
 <!--
         1.如果为空,显示为空
         2.如果不为空,以表单的形式显示
         3.可以用jstl简单的标签代替
          -->
 <c:if test="${empty requestScope.emps}">
  <h1 align="center">THERE IS NO EMPLOYEE</h1>
 </c:if>
 <c:if test="${!empty requestScope.emps}">
  <h1 align="center">ALL EMPLOYEES</h1>
  <table width="500px" border="1" align="center">
   <tr>
    <td>ID</td>
    <td>NAME</td>
    <td>MAIL</td>
    <td>GENDER</td>
    <td>DEPARTMENT</td>
    <td>DELETE</td>
    <td>UPDATE</td>
   </tr>
   <c:forEach items="${requestScope.emps}" var="emp">

    <tr>
     <td>${emp.id}</td>
     <td>${emp.name}</td>
     <td>${emp.mail}</td>
     <td>${emp.gender==0?‘MALE‘:‘FEMALE‘}</td>
     <td>${emp.dept.deptName}</td>
     <td><a href="emp/${emp.id}">DELETE</a></td>
     <td><a href="addEmp/${emp.id} ">UPDATE</a></td>
    </tr>

   </c:forEach>
  </table>
 </c:if>
 <!--
                      如何将一个get请求转化为springmvc的DELETE和PUT请求
         POST->DELETE PUT
         HiddenHttpMethodFilter post _method DELET
  -->
 <form action="" method="post">
  <input type="hidden" name="_method" value="delete" />
 </form>
 <!-- 关于update
     1.完成修改页面的跳转(无需再写一个类似于add.jsp的页面,只需回显)
     2.完成真正意义上的修改
 
  -->
</body>
</html>

springmvc-2(rest风格的增删改查)-代码(2)

标签:success   lin   请求   w3c   version   XML   tags   1.0   spn   

原文地址:http://www.cnblogs.com/wantao/p/7988071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!