下面链接为新手刚接触时做的配置,但是这种方法并不是在实际开发所需要的代码格式,接下来演示项目中基础代码的实现流程
项目中出现Spring和SpringMVC时,如何进行配置?_不想睡醒的梦的博客-CSDN博客
演示在开发过程中的代码演示,在日常的开发过程中我们并不会像为每个类创建<bean>方法,这些内容几乎都是靠注解的方式去实现,再通过组件扫描功能为每个类自动去创建对象,在SpringMVC的开发过程中我们要配置处理器适配器 处理器映射器等,今天讲解的就是在SpringMVC框架下的最终实现,基本上以下内容就是在日常开发中所使用的代码的最终实践。
处理器映射器:HandlerMapping
之前在使用处理器映射器时,使用的BeanNameUrlHandlerMapping 处理器映射器,但是这种方法不能实现注解方式,所以我们需要通过新的类去实现在项目中创建的注解对象RequestMappingHandlerMapping这个类可以匹配@RequestMapping()
处理器适配器:HandlerAdapter
在注解方法中使用RequestMappingHandlerAdapter来实现处理器适配器
代码实现
配置在SpringMVC框架的环境配置
<?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/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 创建组件扫描功能-->
<context:component-scan base-package="controller" >
</context:component-scan>
<!-- 创建处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping"></bean>
<!-- 创建处理适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" id="handlerAdapter"></bean>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="modelAndView">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
创建Controller类,并通过注解的方式
@Controller //注解通过扫描组件为这各类创建对象
public class Controller4 {
@RequestMapping("/hello") //注解方法实现请求,/hello4是这个请求的URL
public ModelAndView demo1(){
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addObject("name","springMVC");
return modelAndView;
}
}
然后就是在web.XML中配置servlet和servletMapping
<?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">
<!-- springmvc的配置内容-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc_demo1.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在pom.XMl中导入SpringMVC中需要的各种依赖
创建一个jsp文件,让项目中的内得以输出
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2022/11/2
Time: 20:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
hello ${name}
</body>
</html>
执行项目:
去浏览执行hello的URL
以上就是在实际开发过程中最最最为基础的框架实现代码
请求窄化
在开发的过程中Controller类可能有很多,怎么通过URL去调用到具体的类呢?请求窄化这个方法就可以更加精准的定位到我们所需要的类,具体实现那就是在类的上面再加一个@RequestMapping注解,并为这个类加一个更加精准的URl
代码:
@Controller
@RequestMapping("/web") //请求窄化的实现代码
//区别在于访问的URL不同,之前是http://localhost:8080/hello,
//但是如果使用的请求窄化这个方法URL为:http://localhost:8080/web/hello
public class controller_Demo1 {
@RequestMapping("/hello")
public ModelAndView demo1(){
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addObject("name","springMVC");
return modelAndView;
}
}
结果:注意荧光笔部分
请求方法限定:
在HTTP请求分为很多种,例如:get方法.post方法等等,在注解@RequestMapping中含有一项参数可以设置那些请求类型能请求这个方法的内容
注意一点:jsp请求只能使用 GET POST HEAD这三种方法,除了这三种方法请求jsp文件,其他的都不能显示jsp的内容
代码:
@Controller
@RequestMapping("/web")
public class controller_Demo1 {
//@RequestMapping(value = "/hello",method = RequestMethod.GET) 这种方法表明只能get方法才能输出 @RequestMapping(value = "/hello",method ={RequestMethod.GET,RequestMethod.HEAD}) 这种数组表示只能使用数组的请求方法才能请求数据
//也可以这样编写
@GetMapping("/hello11")
public ModelAndView demo1(){
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addObject("name","springMVC");
return modelAndView;
}
}