当前位置: 代码迷 >> 综合 >> SpringMVC 使用@RequestMapping 控制器注解实例 (IntelliJ IDEA)
  详细解决方案

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

热度:40   发布时间:2023-12-26 13:14:37.0

1. 项目目录

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

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: YimsoDate: 2019/3/2Time: 10:48To 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: YimsoDate: 2019/3/1Time: 16:25To 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,运行结果如图所示:

 

3.2 测试helloUesr

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

END。

  相关解决方案