@RequestMapping学习--01(在类上和在方法上)

一  @RequestMapping

在类上定义:提供初求映射信息。相WEB 用的根目

在方法上定义:提供分映射信息。相义处URL

下面是一个例子

首先 1.导入相关的jar包

commons-logging-1.1.3.jar
– spring-aop-4.0.0.RELEASE.jar
– spring-beans-4.0.0.RELEASE.jar
– spring-context-4.0.0.RELEASE.jar
– spring-core-4.0.0.RELEASE.jar
– spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.ja

  2.修改web.xml(在web.xml文件中添加 dispatcherServlet


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

3.配置springmvc.xml文件



	
	
	
	
		
		
	
	
	

4. 写一个类来测试

package org.springmvc;

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


@Controller
public class HelloWorld {
	
	@RequestMapping(value="/helloA")
	public String hello(){
		System.out.println("helloword");
		return "success";
	}
}

测试的目录结构和hello.jsp   

@RequestMapping学习--01(在类上和在方法上)_第1张图片

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




Insert title here


	helloworld

success.jsp

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




Insert title here


		

yes Success

启动tomcat  访问http://localhost:8080/springMVC-01/hello.jsp

@RequestMapping学习--01(在类上和在方法上)_第2张图片

点击helloworld  跳转到http://localhost:8080/springMVC-01/helloA   

这里的helloA是RequestMapping里面的    .jsp作为后缀         

@RequestMapping学习--01(在类上和在方法上)_第3张图片

若在测试类上添加@requestMapping

@RequestMapping("/cccc")   
@Controller
public class HelloWorld {
	
	@RequestMapping(value="/helloA")
	public String hello(){
		System.out.println("helloword");
		return "success";
	}
}

则必须要访问   http://localhost:8080/springMVC-01/cccc/helloA -------才可以访问到  success.jsp

你可能感兴趣的:(javaweb)