【Java学习20170524】SpringMVC

SpringMVC总结


SpringMVC
1、配置web.xml,用一个servlet拦截所有请求【.do】,如果需要设置配置文件的位置及名称
2、创建springMVC的配置文件
1)、将uri连接对应的控制器  
HandlerMapping
HandlerAdapter
2)、给控制器设置资源视图的位置【前缀,后缀】

SpringMVC_1

配置web.xml



	SpringMVC_1
	
	
		
		SpringMVC_1
		
		org.springframework.web.servlet.DispatcherServlet
		1
	

	
		
		SpringMVC_1
		
		/
	
	
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	

配置SpringMVC_1-servlet.xml





	
	
	

	
	
	
	
	
	
		
		
		
		
		
		
	



controller

package com.share.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class MyController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		//ModelAndView view = new ModelAndView();
		//setViewName():相当于请求转发, 这是转发到helloword中
		// /WEB-INF/jsp/helloword.jsp
		//view.setViewName("helloword");
		
		ModelAndView view = new ModelAndView("helloword");
		return view;
	}

}

SpringMVC_2

配置web.xml



  SpringMVC_2
  
    spring
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:config/spring-mvc.xml
    
    1
  
  
    spring
    /
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
配置spring-mvc.xml





	
	
	
	
		
			
				
				abc
				second
			
		
	

	
	
	
	
	
	
	
		
		
		
		
		
		
	



controller

package com.share.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class MyController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		//ModelAndView view = new ModelAndView();
		//setViewName():相当于请求转发, 这是转发到helloword中
		// /WEB-INF/jsp/helloword.jsp
		//view.setViewName("helloword");
		
		ModelAndView view = new ModelAndView("helloword");
		return view;
	}
}

package com.share.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class Second implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		ModelAndView view = new ModelAndView();
		view.setViewName("second");
		return view;
	}

}

SpringMVC_3  注解配置

web.xml



  SpringMVC_3
  
  
    spring
    org.springframework.web.servlet.DispatcherServlet
    
    	
      contextConfigLocation
      classpath:config/spring-mvc.xml
    
    1
  
  
    spring
    /
  
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
spring-mvc.xml





	
	
	
	
	
	



	
	
		
		

		
		
		
	




controller

package com.share.controller;

import javax.servlet.http.HttpServletRequest;

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

/**
 * @Controller:当前类是控制器
 * */
@Controller
public class MyController {
	/**
	 * @RequestMapping:设置控制器的访问路径
	 * 浏览器通过/hello直接可访问
	 * */
	@RequestMapping(value = "/hello")
	public ModelAndView abc(){
		System.out.println("这是注解的控制器");
		ModelAndView view = new ModelAndView();
		//转发到first
		view.setViewName("first");
		return view;
	}
	/**
	 * 通过浏览器直接访问/second
	 * */
	@RequestMapping(value = "/second")
	public ModelAndView bcd(){
		ModelAndView view = new ModelAndView();
		view.setViewName("sencond");
		System.out.println("第二个页面");
		return view;
	}
}




你可能感兴趣的:(Java学习)