SSM中的Restful请求

下面加了Restful标识的是Restful风格必须的配置

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>Restful</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<!--解决post乱码-->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-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.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>  <!-- 启动等级, 随着容器一起启动 -->
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern><!--拦截所有,Restful请求-->
  </servlet-mapping>


  <!-- Restful 配置请求监听 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>
</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-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="hx.insist"></context:component-scan>

    <!-- 配置视图解析器参数 -->
    <bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- Restful 当请求404时去找静态页面 -->
    <mvc:default-servlet-handler />
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--因为DispatcherServlet的url-pattern配置为’/’, 把所有请求都交给了springMVC,
    但是静态资源没有在控制器中做映射,会显示404,
     配置mvc:default-servlet-handler则用Web应用服务器默认的Servlet处理-->
</beans>

controller

package hx.insist.controller;


import hx.insist.dao.DepartmentDao;
import hx.insist.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

@Controller
public class EmployeeHandler {

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private DepartmentDao departmentDao;

}

get

页面:a标签的href默认请求是get方式

<a href="emps">list all employees</a> <br>

controller:

@RequestMapping(value = "/emps", method= RequestMethod.GET)
public String list(Map<String, Object> map){
    map.put("employees", employeeDao.getAll());
    //map.put("employees", null);
    return "list";
}

在controller中保存的数据,要从requestScope域中获取

页面:

<div>
   <%-- request和requestScope其实是一样的吗
   <c:if test="${empty request.employees }">
        没有员工信息
    </c:if>--%>
    <c:if test="${empty requestScope.employees }">
        没有员工信息
    </c:if>
    <c:if test="${!empty requestScope.employees }">
        <table border="1" >
            <tr>
                <th>ID</th>
                <th>lastName</th>
                <th>email</th>
                <th>gender</th>
                <th>department</th>
                <th>edit</th>
                <th>delete</th>
            </tr>

            <c:forEach items="${requestScope.employees }" var="emp">
                <tr>
                    <td>${emp.id }</td>
                    <td>${emp.lastName }</td>
                    <td>${emp.email }</td>
                    <td>${emp.gender == 0 ? 'Female' : 'Male' }</td>
                    <td>${emp.department.departmentName }</td>
                    <td><a href="">Edit</a></td>
                    <!-- 因为a标签是get请求, 要转成Delete请求, 可以使用jquery -->
                    <td><a class="delete" href="">Delete</a></td>
                </tr>
            </c:forEach>
        </table>
    </c:if>
</div>
insist,on the road
-------------本文结束感谢您的阅读-------------