当前位置:   article > 正文

@RequestMapping学习--01(在类上和在方法上)_requestmapping在方法上和在类上有什么区别

requestmapping在方法上和在类上有什么区别

一  @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

  1. <servlet>
  2. <servlet-name>dispatchServlet</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:springmvc.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>dispatchServlet</servlet-name>
  12. <url-pattern>/</url-pattern>
  13. </servlet-mapping>

3.配置springmvc.xml文件

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

4. 写一个类来测试

  1. package org.springmvc;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. public class HelloWorld {
  6. @RequestMapping(value="/helloA")
  7. public String hello(){
  8. System.out.println("helloword");
  9. return "success";
  10. }
  11. }

测试的目录结构和hello.jsp   


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

success.jsp

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

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


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

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


若在测试类上添加@requestMapping

  1. @RequestMapping("/cccc")
  2. @Controller
  3. public class HelloWorld {
  4. @RequestMapping(value="/helloA")
  5. public String hello(){
  6. System.out.println("helloword");
  7. return "success";
  8. }
  9. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/194587
推荐阅读
  

闽ICP备14008679号