SpringMVC基本使用
程序员文章站
2022-03-19 15:59:14
SpringMVC工作流程SpringMVC基本使用xml模式(麻烦,不推荐)1.1导入依赖在pom.xml中添加spring-mvc,servlet,jsp的依赖: org.springframework spring-webmvc
SpringMVC工作流程
SpringMVC基本使用
xml模式(麻烦,不推荐)
1.1导入依赖
在pom.xml中添加spring-mvc,servlet,jsp的依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
添加了spring-webmvc依赖之后,其他的spring-web,sping-aop,spring-context等等就全部加进来了
1.2配置核心(前端)控制器
web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<!--配置DispatcherServlet:SpringMVC的核心;请求分发器,前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--绑定SpringMVC的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别 饿汉,懒汉式加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
在SpringMVC中,/ ,/*
/:只匹配所有的请求,不匹配静态资源,会覆盖掉TOMCAT原本的默认Servlet
/*:匹配所有的请求,包括静态资源
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置SpringMVC的乱码过滤-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.3配置文件
resources文件夹下建立对应配置文件,根据前端控制器绑定的配置文件名
新建 springmvc-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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--3.配置控制器 相当于配置了访问路径 -->
<bean id="/hello" class="com.peppa.web.HelloController"/>
</beans>
1.4后端控制器
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView();
//业务代码
String result="HelloSpringMVC";
modelAndView.addObject("msg",result);
//视图跳转
modelAndView.setViewName("test");
return modelAndView;
}
}
1.5访问
注解模式(推荐)
导jar和web.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置注解扫描位置-->
<context:component-scan base-package="com.peppa.web.controller"/>
<!--省去处理器和映射器配置,注解配置驱动-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
控制层的代码
@Controller //控制器
@RequestMapping("/Hello") //这个是为了分清模块
public class StudentController {
@RequestMapping("/test1") //链接名
public String list(){
System.out.println(1);
return "/index"; //返回的页面
}
//这个为ajax的套路
@RequestMapping("/test2")
public void list2(HttpServletRequest request, HttpServletResponse response) throws IOException {
String str=JSON.toJSONString("ssssss");
response.getWriter().println(str);
response.getWriter().flush();
response.getWriter().close();
}
}
本文地址:https://blog.csdn.net/qq_44073614/article/details/111838085
上一篇: 真的用不上,排序算法
下一篇: 二、关于Tomcat(内附配置流程)