SpringMVC的环境搭建

搭建方式:一、配置形式

1、web.xml的配置



  SpringMVCDemo02	
  
  	spring
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  		classpath*:applicationContext_mvc.xml
  	
  	1
  
  
  	spring
  	*.do
  
  
    index.jsp
  

load-on-startup元素标记容器是否在启动的时候就加载这个servlet
1)它的值必须是一个整数,表示servlet应该被载入的顺序
2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
5)当值相同时,容器就会自己选择顺序来加载。
DispatcherServlet:解析请求映射的找到application-mvc.xml
SimpleUrlHandelMapping:建立请求和action或者controller之间的映射

 
  
 
  
2、
 
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	   xmlns:mvc="http://www.springframework.org/schema/mvc" 
	   xmlns:context="http://www.springframework.org/schema/context" 
	   xsi:schemaLocation="http://www.springframework.org/schema/beans 
	   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
	   http://www.springframework.org/schema/context 
	   http://www.springframework.org/schema/context/spring-context-4.2.xsd 
	   http://www.springframework.org/schema/mvc 
	   http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
	  
	   
	   
	   
	   
	   		
	   		
	   
	  
	   
	    
	   		
	   			
	   				testAction
	   			
	   		
	   
	   
package com.javalxj.spring.mvc.action;


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 TestAction implements Controller{
/**
 * 默认方法
 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("testAction"+request.getParameter("userName"));
		return new ModelAndView("index");
	}


	


}


二、注解形式


	  
	   
	   
	   
	   
	   		
	   		
	   
@Controller
public class TestController {
		//用于处理响应
	@RequestMapping("/login.do")
	public String login(String userName,String userPwd,Map map){
		if("admin".equals(userName)&&"123".equals(userPwd)){
			map.put("userName",userName);
			System.out.println("testController delete mathod:"+userName+":"+userPwd);
			return "success";
		}
		System.out.println("testController delete mathod:"+userName+":"+userPwd);
		return "login";
		




你可能感兴趣的:(Spring)