SpringMVC--RESTful入门

主要内容:

SpringMVC--RESTful入门_第1张图片


RESTful概述:

SpringMVC--RESTful入门_第2张图片


 RESTful核心内容:

SpringMVC--RESTful入门_第3张图片


RESTful架构特点:

SpringMVC--RESTful入门_第4张图片


RESTful风格概述:

SpringMVC--RESTful入门_第5张图片

SpringMVC--RESTful入门_第6张图片

SpringMVC--RESTful入门_第7张图片


案例:课程添加删除

web.xml配置过滤器:

  
    hiddenHttpMethodFilter
    org.springframework.web.filter.HiddenHttpMethodFilter
  

  
    hiddenHttpMethodFilter
    /*
  

业务代码:

Entity:

Course:

package com.imooc.entity;
public class Course {
    private int id;
    private String name;
    private double price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

Dao:

CourseDao:

package com.imooc.dao;

import com.imooc.entity.Course;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class CourseDAO {
    private Map courses = new HashMap();

    /**
     * 新增课程
     */
    public void add(Course course){
        courses.put(course.getId(),course);
    }

    /**
     * 查询全部课程
     */
    public Collection getAll(){
        return courses.values();
    }

    /**
     * 通过id查询课程
     */
    public Course getById(int id){
        return courses.get(id);
    }

    /**
     * 修改课程
     */
    public void update(Course course){
        courses.put(course.getId(),course);
    }

    /**
     * 删除课程
     */
    public void deleteById(int id){
        courses.remove(id);
    }
}

Controller:

CourseController:

package com.imooc.controller;

import com.imooc.dao.CourseDAO;
import com.imooc.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator.
 */
@Controller
public class CourseController {
    @Autowired
    private CourseDAO courseDAO;

    /**
     * 添加课程
     */
    @PostMapping(value = "/add")
    public String add(Course course){
        courseDAO.add(course);
        return "redirect:/getAll";
    }

    /**
     * 查询全部课程
     * @return
     */
    @GetMapping(value = "/getAll")
    public ModelAndView getAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("courses",courseDAO.getAll());
        return modelAndView;
    }

    /**
     * 通过id查询课程
     */
    @GetMapping(value = "/getById/{id}")
    public ModelAndView getById(@PathVariable(value = "id") int id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("edit");
        modelAndView.addObject("course",courseDAO.getById(id));
        return modelAndView;
    }

    /**修改课程
     *
     */
    @PutMapping(value = "/update")
    public String update(Course course){
        courseDAO.update(course);
        return "redirect:/getAll";
    }

    /**
     * 删除课程
     */
    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable(value = "id")  int id){
        courseDAO.deleteById(id);
        return "redirect:/getAll";
    }

}

配置文件:

springmvc.xml:




    
        
        
            
        
    

	
    

    
        
        
    

web.xml:




  Archetype Created Web Application

  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    encodingFilter
    /*
  

  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
  
  
    springmvc
    /
  

  
    default
    *.css
  

  
    hiddenHttpMethodFilter
    org.springframework.web.filter.HiddenHttpMethodFilter
  

  
    hiddenHttpMethodFilter
    /*
  

JSP页面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>


    add
    
    



imooc-添加课程

edit.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>


    add
    
    



imooc-修改课程

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


    课程列表
    


imooc-课程管理

编号 课程名 价格 编辑 删除
${course.id} ${course.name} ${course.price}

 

 

你可能感兴趣的:(SpringMVC--RESTful入门)