在idea中,依旧使用maven项目(前面详解)
步入主题,springMVC主要就是封装了servlet,以DispatcherServlet的形式出现在配置文件中,接下来进行详细的配置
目录视图展示,防止在讲哪个文件在哪里时出现懵b状态
1,导入依赖
springMVC可以说就是一个spring的一种具体实现,因此需要spring的核心包,还需要servlet以及jsp的核心包,可以使用juint测试
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
2,在web.xml中配置文件,需要配置dispatcherServlet文件以及字符过滤器
<?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" metadata-complete="true"> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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> </web-app>
3,在这个配置web配置文件中,在servlet里多加了以下这么一段话,主要就是将该配置较给spring容器管理
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC-servlet.xml</param-value> </init-param>
4,在这个springMVC-servlet.xml容器中,需要对实体类进行扫描以便使用注解,而在springMVC中有三大要素
<!--Spring三要素--> <!--处理器映射器--> <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"> 交于spring管理
这里使用注解实现,因此在spring中,只需要进行如下配置
mvc:annotation-driven</mvc:annotation-driven>:表示将处理器的映射器和适配器自动帮我们配好
mvc:default-servlet-handler</mvc:default-servlet-handler>: 过滤非文本文件
<property name="prefix" value="/WEB-INF/jsp/"></property>: 前缀 <property name="suffix" value=".jsp"></property>:后缀 用于拼接最终访问的路径
<?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:mvc="http://www.springframework.org/schema/mvc" 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 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.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler></mvc:default-servlet-handler> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
5,编写实体类ControllerQaq
最终返回的test为全路径 相当于:/WEB-INF/jsp/test.jsp,只需要加上前面的前缀和后缀
package com.controller; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/qaq") //浏览器访问的映射路径 public class ControllerQaq { @GetMapping("/c1") //与上面的requestMapping的映射路径一致,只不过声明了get与post的访问方式 public String test(Model model,String name,String age,String password){ //model:用来携带数据 model.addAttribute("name",name); model.addAttribute("age",age); model.addAttribute("password",password); System.out.println("数据传送成功!"); //浏览器做出响应后返回的页面 return "test"; } }
6,编写user.jsp
如下可以发现,上面的两个映射路径在下面表单提交时跳转,这样才能通过访问这个映射路径再次访问到用拼接起来的test.jsp路径下
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div align="center"> <form method="get" action="/qaq/c1"> <br> <br> 姓名为:<input type="text" name="name"> <br> <br> 年龄为:<input type="text" name="age"> <br> <br> 密码:<input type="password" name="password"> <br> <br> <input type="submit" value="提交"> </form> </div> </body> </html>
7,编写最终到达的test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>springMVC</title> </head> <body> <div align="center"> <br> <br> <br> <table border="1"> <tr> <td width="40%">姓名为:</td> <td width="60%">${name}</td> </tr> <tr> <td width="40%">年龄为:</td> <td width="60%">${age}</td> </tr> <tr> <td width="40%">密码为:</td> <td width="60%">${password}</td> </tr> </table> </div> </body> </html>
8,编辑tomcat,容易出错的问题就是需要将更改模块的配置,如果模块多的话;其他的应该不难.
9,测试
点击tomcat运行就行,在输入路径user.jsp,输入各个参数就能跳转到最终的界面
springMVC搭建成功!