제2 장 Spring MVC 추가 검사 사례

12110 단어 springmvc
더 읽 기
이 장 은 spring mvc 를 사용 하여 student 대상 의 첨삭 을 모 의 하 는 작은 예 입 니 다.
1.spring mvc 주석:
1.@Controller 성명 컨트롤 러


@Controller
public class StudentController {
    //....
}


2,@RequestMapping 요청 맵/요청 경로


@RequestMapping("/student")
public class StudentController {
    //    ,    /student


    @RequestMapping("/list")
    public ModelAndView list() {
        //    ,    /student/list
    }


}




3,@RequestParam 요청 매개 변수


@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value = "id", required = false) String id){
    //  id
}

@RequestMapping("/save")
public ModelAndView save(Student student){
    //      ,    
}


4.Model AndView 는 모델 과 보 기 를 되 돌려 줍 니 다.


@RequestMapping("/delete")
public ModelAndView delete(Student student) {
	studentMap.remove(student.getId());
	ModelAndView mav = new ModelAndView();
	mav.addObject("studentList", studentMap);
	mav.setViewName("student/list");
	return mav;
}


2.spring 중국어 인 코딩 필터,웹.xml 에 추가;

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


3.jsp 는 HashMap 의 대상 을 옮 겨 다 니 며 대상 의 속성 을 가 져 옵 니 다.


	
	
		${student.value.id }
		${student.value.name }
		${student.value.age }
		
			수정 하 다.
			    
			삭제
		
	
	


4.Model AndView 는 모델 과 보 기 를 되 돌려 줍 니 다.


	/**
	 *          
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/preSave")
	public ModelAndView preSave(
			@RequestParam(value = "id", required = false) String id) {
		ModelAndView mav = new ModelAndView();
		if (id != null) {
			mav.addObject("student", studentMap.get(Integer.parseInt(id)));
			mav.setViewName("student/update");
		} else {
			mav.setViewName("student/add");
		}
		return mav;
	}


5.SpringMVC 대상 속성 자동 봉인


	/**
	 *   ,    ,   
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Student student) {
		if (student.getId() == null) {
			//   id   1
			identityId += 1;
			student.setId(identityId);
			studentMap.put(student.getId(), student);
		} else {
			//            
			studentMap.put(student.getId(), student);
		}
		//  
		return "redirect:/student/list.do";
		//   
		// return "redirect:/student/list.do";
	}


6.내부 재 설정 과 전송:


	/**
	 *   
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam("id") Integer id) {
		studentMap.remove(id);
		//  
		return "redirect:/student/list.do";
		//   
		// return "redirect:/student/list.do";
	}


프로젝트 원본:
web.xml




	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		encodingFilter
		/*
	
	SpringMvc02
	
		index.jsp
	
	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
	
	
	
		springmvc
		*.do
	



spring-mvc.xml





	
    

    
	
		
		
	




Student.java


package com.fx.model;

public class Student {

	private Integer id;
	private String name;
	private Integer age;

	public Student() {
		super();
	}

	public Student(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}



StudentController.java


package com.fx.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.fx.model.Student;

/**
 * Controller:  Controller    RequestMapping:      
 * 
 * @author fx
 *
 */
@Controller
@RequestMapping("/student")
public class StudentController {

	private Integer identityId = 3;

	private static Map studentMap = new HashMap();

	static {
		studentMap.put(1, new Student(1, "  ", 11));
		studentMap.put(2, new Student(2, "  ", 12));
		studentMap.put(3, new Student(3, "  ", 13));
	}

	/**
	 *     
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("studentList", studentMap);
		mav.setViewName("student/list");
		return mav;
	}

	/**
	 *          
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/preSave")
	public ModelAndView preSave(
			@RequestParam(value = "id", required = false) String id) {
		ModelAndView mav = new ModelAndView();
		if (id != null) {
			mav.addObject("student", studentMap.get(Integer.parseInt(id)));
			mav.setViewName("student/update");
		} else {
			mav.setViewName("student/add");
		}
		return mav;
	}

	/**
	 *   ,    ,   
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Student student) {
		if (student.getId() == null) {
			//   id   1
			identityId += 1;
			student.setId(identityId);
			studentMap.put(student.getId(), student);
		} else {
			//            
			studentMap.put(student.getId(), student);
		}
		//  
		return "redirect:/student/list.do";
		//   
		// return "redirect:/student/list.do";
	}

	/**
	 *   
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam("id") Integer id) {
		studentMap.remove(id);
		//  
		return "redirect:/student/list.do";
		//   
		// return "redirect:/student/list.do";
	}
}




list.jsp








Insert title here


학생
번호
성명.
나이.
조작 하 다.
${student.value.id }
${student.value.name }
${student.value.age }
수정 하 다.
    
삭제
add.jsp







Insert title here


학생
성명.
나이.
update.jsp







Insert title here


학생 수정
성명.
나이.
접근 경로:
http://localhost:8080/SpringMvc02/student/list.do
  • SpringMvc02.zip (7.8 MB)

  • 다운로드 횟수:0

    좋은 웹페이지 즐겨찾기