码迷,mamicode.com
首页 > 编程语言 > 详细

IntellliJ IDEA+maven+spring+springMVC+tomcat搭建本地开发环境(一)

时间:2020-02-14 01:20:24      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:classpath   java web   scan   tle   加载   info   intern   encoding   ems   

1.新建maven项目,选择如下选项,然后进入下一步

技术图片

 

2.输入项目相应的信息,进入下一步

技术图片

 

3.配置maven环境,其中有个下载依赖包失败的问题,下篇将提到解决

技术图片

 

4.点击finish,等待maven下载相应的jar包,创建src/main/webapp/WEB-INF/web.xml,src/main/java,src/main/resources,项目结构如下:

技术图片

 

5.引入java web项目相关到spring和springmvc的jar依赖,pom文件中添加如下:

 1     <dependency>
 2     <groupId>org.springframework</groupId>
 3     <artifactId>spring-core</artifactId>
 4     <version>${spring.version}</version>
 5   </dependency>
 6     <dependency>
 7       <groupId>org.springframework</groupId>
 8       <artifactId>spring-context</artifactId>
 9       <version>${spring.version}</version>
10     </dependency>
11     <dependency>
12       <groupId>org.springframework</groupId>
13       <artifactId>spring-webmvc</artifactId>
14       <version>${spring.version}</version>
15     </dependency>
16     <dependency>
17       <groupId>org.springframework</groupId>
18       <artifactId>spring-web</artifactId>
19       <version>${spring.version}</version>
20     </dependency>
21     <dependency>
22       <groupId>org.springframework</groupId>
23       <artifactId>spring-orm</artifactId>
24       <version>${spring.version}</version>
25     </dependency>
26     <dependency>
27       <groupId>org.springframework</groupId>
28       <artifactId>spring-beans</artifactId>
29       <version>${spring.version}</version>
30     </dependency>
31     <dependency>
32       <groupId>org.springframework</groupId>
33       <artifactId>spring-aop</artifactId>
34       <version>${spring.version}</version>
35     </dependency>

 

6.在resources文件夹创建applicationContext.xml和dispatcher-servlet.xml,其中applicationContext.xml是spring上下文的配置文件,dispatcher-servlet.xml是springMVC的配置文件

 

7.配置web.xml,这里有问题困扰了很久,下章会讲到

 1 <!DOCTYPE web-app PUBLIC
 2         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3         "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 5         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
 7     <!--加载spring上下文-->
 8     <!--配置springIoC容器的配置文件路径-->
 9     <context-param>
10         <param-name>contextConfigLocation</param-name>
11         <param-value>classpath:*applicationContext.xml</param-value>
12     </context-param>
13     <!--配置ContextLoaderListener用以初始化springIoc容器-->
14     <listener>
15         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16     </listener>
17 
18     <!--配置springmvc-->
19     <!--配置DispatcherServlet-->
20     <servlet>
21         <servlet-name>dispatcher</servlet-name>
22         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
23         <init-param>
24             <param-name>contextConfigLocation</param-name>
25             <param-value>classpath:*dispatcher-servlet.xml</param-value>
26         </init-param>
27         <load-on-startup>1</load-on-startup>
28     </servlet>
29     <!--servlet拦截配置,拦截以后缀“from”结束到请求-->
30     <servlet-mapping>
31         <servlet-name>dispatcher</servlet-name>
32         <url-pattern>/</url-pattern>
33     </servlet-mapping>
34 </web-app>

 

8.配置applicationContext.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xmlns:context="http://www.springframework.org/schema/context"
5        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">
6 
7     <!--使用注解驱动-->
8     <context:annotation-config></context:annotation-config>
9 </beans>

 

9.配置dispatcher-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:mvc="http://www.springframework.org/schema/mvc"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:p="http://www.springframework.org/schema/p"
 7        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">
 8 
 9     <!--使用注解驱动-->
10     <mvc:annotation-driven></mvc:annotation-driven>
11     <!--定义扫描装载的包-->
12     <context:component-scan base-package="com.example.ivy"/>
13     <!--定义视图解析图-->
14     <bean id="viewResolver"
15           class="org.springframework.web.servlet.view.InternalResourceViewResolver"
16     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>
17 </beans>

 

10.配置tomcat,右上角Add Configuration,点左上角的加号,选择tomcat server,local,进入tomcat配置页面

技术图片

技术图片

 

11.编写Controller

 1 package com.example.ivy;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 @Controller
 8 @RequestMapping("/example")
 9 public class MyController {
10     @RequestMapping("/test")
11     public ModelAndView test(){
12         ModelAndView mv = new ModelAndView();
13         mv.setViewName("test");
14         return mv;
15     }
16 }

 

12.创建webapp/WEB-INF/jsp/test.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Lai_Ivy
 4   Date: 2020/2/13
 5   Time: 21:33
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>七宝</title>
12 </head>
13 <body>
14 <h1>nihao</h1>
15 
16 </body>
17 </html>

 

运行tomcat能看到后台结果

技术图片

或者更直观,打开http://localhost:8080/example/test,看到页面

技术图片

IntellliJ IDEA+maven+spring+springMVC+tomcat搭建本地开发环境(一)

标签:classpath   java web   scan   tle   加载   info   intern   encoding   ems   

原文地址:https://www.cnblogs.com/ivy-xu/p/12305866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!