@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

<servlet>
 		<servlet-name>dispatchServlet</servlet-name>
 		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 		<init-param>
 			<param-name>contextConfigLocation</param-name>
 			<param-value>classpath:springmvc.xml</param-value>
 		</init-param>
 		<load-on-startup>1</load-on-startup>
 	</servlet>
 	
 	<servlet-mapping>
 		<servlet-name>dispatchServlet</servlet-name>
		<url-pattern>/</url-pattern>
 	</servlet-mapping>

3.配置springmvc.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 配置Controller扫描 -->
	<context:component-scan base-package="org.springmvc" />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

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   


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="helloA">helloworld</a><br>   
	
	
	<!-- <a href="helloAAA">test</a><br>
	
	<form action="helloAAA" method="POST">
		<input type="submit" value="POST"/>
	</form>
	<br>
	<a href="testPH/?username=atguigu&age=40">testParams and headers</a><br> -->
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<h1>yes Success</h1>
</body>
</html>

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


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

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


若在测试类上添加@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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值