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

1. 项目目录

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

SpringMVC 使用@RequestMapping 控制器注解实例 (IntelliJ IDEA)_第1张图片

2. 源文件的编写

2.1 xml 文件配置

web.xml:



  
    contextConfigLocation
    /WEB-INF/applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    1
  

  
  
    dispatcher
    *.form
  
  
  
    index.jsp
  

dispatcher-servlet.xml:




  
  

applicationContext.xml:




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" %>


    Title


${message}

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" %>


    Title


${message}

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,运行结果如图所示:

 

3.2 测试helloUesr

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

END。

你可能感兴趣的:(#,----SpringMVC,SpringMVC学习笔记)