SpringMVC 使用@RequestMapping 控制器注解实例 (IntelliJ IDEA)
  anLrwkgbyYZS 2023年12月30日 18 0


1. 项目目录

首先你需要会创建springMVC的入门工程(SpringMVC入门实例,点击前往),工程的项目目录如下图所示。

SpringMVC 使用@RequestMapping 控制器注解实例 (IntelliJ IDEA)_spring

2. 源文件的编写

2.1 xml 文件配置

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- SpringMVC 的前端控制器 -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- 拦截了以 ".form" 结尾的URL请求,Web 应用接收到该类请求之后 , 会调用前端控制器-->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <!-- 扫描配置 -->
  <context:component-scan base-package="com.study.springmvc.contoller"></context:component-scan>

</beans>

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

2.2 jsp文件配置

helloUser.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Yimso
  Date: 2019/3/2
  Time: 10:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>

helloWorld.jsp

<%--
  Created by IntelliJ IDEA.
  User: Yimso
  Date: 2019/3/1
  Time: 16:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>

2.3 java 源文件编写:

HelloUserController.java:

package com.study.springmvc.contoller;

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

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

// @Controller 标识是一个控制器
@Controller
public class HelloUserController {

    // url请求中必须包含一个 username 的参数
    @RequestMapping(value = "/helloUser", params = "username")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 创建模型
        ModelAndView modelAndView = new ModelAndView();

        // 向 模型 中添加数据
        modelAndView.addObject("message", "Hello," + request.getParameter("username") + "!");

        // 返回逻辑视图
        modelAndView.setViewName("/WEB-INF/jsp/helloUser.jsp");
        return modelAndView;
    }
}

HelloWorldController.java:

package com.study.springmvc.contoller;

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

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

// @Controller 标志是一个控制器
@Controller
public class HelloWorldController{

    @RequestMapping("helloWorld")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 创建模型
        ModelAndView modelAndView = new ModelAndView();

        // 向 模型 中添加数据
        modelAndView.addObject("message", "Hello World");

        // 返回逻辑视图
        modelAndView.setViewName("/WEB-INF/jsp/helloWorld.jsp");
        return modelAndView;
    }
}

test.java

package com.study.springmvc;

public class test {
    // empty
}

3 项目测试

3.1 测试helloWorld

访问 http://localhost:8080/helloWorld.form,运行结果如图所示:

SpringMVC 使用@RequestMapping 控制器注解实例 (IntelliJ IDEA)_java_02

 

3.2 测试helloUesr

访问http://localhost:8080/helloUser.form?username=springmvc,运行结果如图所示:

SpringMVC 使用@RequestMapping 控制器注解实例 (IntelliJ IDEA)_spring_03

END。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月30日 0

暂无评论

推荐阅读
  bVJlYTdzny4o   3天前   8   0   0 Java
anLrwkgbyYZS