乐趣区

关于后端:SSM系列教材-九-restful-风格的SSM项目


步骤 1:Restful 格调是什么
步骤 2: 先运行,看到成果,再学习
步骤 3:web.xml
步骤 4:listCategory.jsp
步骤 5:editCategory.jsp
步骤 6:CategoryController

步骤 1 : Restful 格调是什么

步骤 2 : 先运行,看到成果,再学习

下载区 (点击进入) 筹备了一个能够运行的 restful 格调的 crud,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的成果。
启动后,测试地址是:

http://127.0.0.1:8080/ssm/categories

注: 这个 restful 格调的 curd,是在传统格调的 crud 的根底上批改了,所以最好先相熟之后,再学习该知识点。

步骤 3 : web.xml

SpringMVC 只能解决 post 和 get, 为了还原对 put 和 delete 的解决,web.xml 外面要加如下过滤器

<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>

<?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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <!-- spring 的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>
    <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>
    <!-- spring mvc 外围:散发 servlet -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- spring mvc 的配置文件 -->
        <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>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

步骤 4 : listCategory.jsp

listCategory.jsp 做了如下批改
1. 减少
action 批改为 ”categories”
2. 删除
2.1 url 批改为 categories/id
2.2 点击超链后,会应用 form 提交,并且提交_method 的值为 delete, 这样在 springmvc 失去后,就会以 method=delete 解决了。

$(function(){$(".delete").click(function(){var href=$(this).attr("href");
         $("#formdelete").attr("action",href).submit();
         return false;
     })
 })

3. 获取
3.1 url 批改为了 /categories/id

4. 在最开始减少了 jquery.min.js 的引入

<script type="text/javascript" src="js/jquery.min.js"></script>`


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<script type="text/javascript" src="js/jquery.min.js"></script>

<script type="text/javascript">
    /* 将 post method 扭转为 delete*/
    $(function(){$(".delete").click(function(){var href=$(this).attr("href");
            $("#formdelete").attr("action",href).submit();
            return false;
        })
    })
</script>

<div style="width:500px;margin:0px auto;text-align:center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>name</td>
            <td> 编辑 </td>
            <td> 删除 </td>
        </tr>
        <c:forEach items="${cs}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.name}</td>
                <td><a href="categories/${c.id}"> 编辑 </a></td>
                <td><a class="delete" href="categories/${c.id}"> 删除 </a></td>
            </tr>
        </c:forEach>
    </table>
    <div style="text-align:center">
        <a href="?start=0"> 首  页 </a>
        <a href="?start=${page.start-page.count}"> 上一页 </a>
        <a href="?start=${page.start+page.count}"> 下一页 </a>
        <a href="?start=${page.last}"> 末  页 </a>
    </div>

    <div style="text-align:center;margin-top:40px">
        <form method="POST" action="categories">
            分类名称:<input name="name" value=""type="text"> <br><br>
            <input type="submit" value="减少分类">
        </form>
    </div>
</div>

<form id="formdelete" action=""method="POST" >
    <input type="hidden" name="_method" value="DELETE">
</form>

步骤 5 : editCategory.jsp

action 批改为了 categories/id
留神:form 下减少 filed, 尽管这个 form 的 method 是 post, 然而 springmvc 看到这个_method 的值是 put 后,会把其批改为 put.

<input type="hidden" name="_method" value="PUT">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div style="width:500px;margin:0px auto;text-align:center">
    <div style="text-align:center;margin-top:40px">
        <form method="post" action="../categories/${c.id}">
            <input type="hidden" name="_method" value="PUT">
            分类名称:<input name="name" value="${c.name}" type="text"> <br><br>
            <input type="submit" value="批改分类">
        </form>

    </div>
</div>

步骤 6 : CategoryController

CRUD 的 RequestMapping 都批改为了 /categories, 只是 method 各不相同。
其余的。。。没变动

package com.how2java.controller;

import java.util.List;
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 org.springframework.web.servlet.ModelAndView;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
// 通知 spring mvc 这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @RequestMapping(value="/categories",method=RequestMethod.GET)
    public ModelAndView listCategory(Page page){ModelAndView mav = new ModelAndView();
        List<Category> cs= categoryService.list(page);
        int total = categoryService.total();
        page.caculateLast(total);
        // 放入转发参数
        mav.addObject("cs", cs);
        // 放入 jsp 门路
        mav.setViewName("listCategory");
        return mav;
    }
    @RequestMapping(value="/categories",method=RequestMethod.POST)
    public ModelAndView addCategory(Category category){System.out.println("category.getName():"+category.getName());
        categoryService.add(category);
        ModelAndView mav = new ModelAndView("redirect:/categories");
        return mav;
    }
    @RequestMapping(value="/categories/{id}",method=RequestMethod.DELETE)
    public ModelAndView deleteCategory(Category category){categoryService.delete(category);
        ModelAndView mav = new ModelAndView("redirect:/categories");
        return mav;
    }
    @RequestMapping(value="/categories/{id}",method=RequestMethod.GET)
    public ModelAndView editCategory(Category category){Category c= categoryService.get(category.getId());
        ModelAndView mav = new ModelAndView("editCategory");
        mav.addObject("c", c);
        return mav;
    }
    @RequestMapping(value="/categories/{id}",method=RequestMethod.PUT)
    public ModelAndView updateCategory(Category category){categoryService.update(category);
        ModelAndView mav = new ModelAndView("redirect:/categories");
        return mav;
    }

}

更多内容,点击理解:https://how2j.cn/k/ssm/ssm-restful/1638.html

退出移动版