第一个SpringMVC程序
2021-06-23 09:05
YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
标签:cat 经典 ati 自定义 经典的 java setting 随笔 png
准备工作:
1、第一步先创建一个maven的工程,选择webapp项目
2、取一个artificts名字spring-mvc一路下一步,反正是要配的,具体artifacts怎么用的我还没弄明白,后面再出个随笔
3、设置目录,点击完成创建
4、将默认的依赖删掉,自己添加springMVC的jar依赖
5、添加相关依赖
dependencies>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-coreartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-beansartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-expressionartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-aopartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-aspectsartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-jdbcartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-txartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
version>3.3.0version>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatis-springartifactId>
version>1.3.1version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webmvcartifactId>
version>5.0.5.RELEASEversion>
dependency>
dependency>
groupId>javax.servletgroupId>
artifactId>javax.servlet-apiartifactId>
version>3.1.0version>
dependency>
dependency>
groupId>javax.servlet.jspgroupId>
artifactId>jsp-apiartifactId>
version>2.2version>
dependency>
dependency>
groupId>jstlgroupId>
artifactId>jstlartifactId>
version>1.2version>
dependency>
dependencies>
这里需要注意有个小技巧,有的时候添加依赖的时候maven显示找不到,带红色波浪线,可以先去百度搜有没有这个版本号,修改版本号重新导入一下
比如查到mybatis-spring的版本号有1.3.1,再在version一栏添加1.3.1
springMVC执行过程:
一、配置核心控制器
在web.xml里面配置
xml version="1.0" encoding="UTF-8"?> web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> servlet> servlet-name>springMVCservlet-name> servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> load-on-startup>1load-on-startup> servlet> servlet-mapping> servlet-name>springMVCservlet-name> url-pattern>/url-pattern> servlet-mapping> web-app>
二、创建并配置核心配置文件
两种定义方式:
- 使用默认位置,在WEB-INF下面创建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" 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 class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> bean name="/hello" class="controller.HelloController"/> bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> property name="prefix" value="/WEB-INF/view/"/> property name="suffix" value=".jsp"/> property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> bean>
三、用实现接口的方式编写一个controller类
package controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @Description:处理请求,返回mav * @Authoor: hujian * @Date: 2018/9/19 13:02 * @Version 1.0.0 */ public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { String name = req.getParameter("name"); ModelAndView mav = new ModelAndView(); mav.addObject("msg","Hello"+name); mav.setViewName("hello"); return mav; } }
四、再新建一个hello.jsp
Created by IntelliJ IDEA. User: Administrator Date: 2018/9/19 Time: 13:06 To change this template use File | Settings | File Templates. --%>Title msg:${msg}
五、启动tomcat
加上/hello
带参数访问controller
到此处第一个MVC程序的过程就完了,第一次写博客,有什么问题多多指教,最后附上一张经典的过程图:
第一个SpringMVC程序
标签:cat 经典 ati 自定义 经典的 java setting 随笔 png
原文地址:https://www.cnblogs.com/waycx/p/9665923.html
上一篇:python使用多进程
下一篇:javaEE项目部署方式