idea使用maven搭建springmvc
最近学着搭建springmvc,写此博客记录一下
idea版本:2016.3.1
maven: apache-maven-3.3.9
tomcat:apache-tomcat-8.5.8
1、New Project
2、next
3、next
上面的maven是我之前配置好的国内镜像,使用原生镜像因为在国外所以会很慢
配置方法 http://www.cnblogs.com/dangzhensheng/p/6217926.html
再点击右下的+号 写入archetypeCatalog=internal 使用本地镜像
4、next
选择存放路径 点Finish 完成
5、
出现此选项选择此项 让maven自动导入包
6、加载好之后的项目结构
7、点击工具栏中Project Structure图片 选择左侧Modules,在Sources中main文件下新建java文件夹,并点击蓝色文件夹Sources,java文件夹变为蓝色设置成功,点击保存即可
8、编辑pom.xml,加载项目所需的jir包,完整内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dang</groupId> <artifactId>WebDemo</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WebDemo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.3.3.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 添加Spring-core包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- 添加spring-context包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- 添加spring-tx包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- 添加spring-jdbc包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- 为了方便进行单元测试,添加spring-test包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!--添加spring-web包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--添加aspectjweaver包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!-- 添加mybatis的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- 添加mybatis与Spring整合的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- 添加servlet3.0核心包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 添加mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- 添加druid连接池包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>WebDemo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
ps:里面加入了mybatis 打算后期用此连接数据库、底部添加resources标签src/main/java是因为现版的idea编译xml时只编译src/main/resources文件下的xml,添加java文件下的xml
9、在src/main/webapp/WEB-INF 下面新建View文件夹和mvc-dispatcher-servlet.xml文件(View用于存放后续视图页面,mvc-dispatcher-servlet.xml用于解析视图文件地址等)
在弹出框中选中mvc-dispatcher-servlet.xml点确定,让idea识别为springmvc的配置文件。
10、修改mvc-dispatcher-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: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/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"> <!--指明 controller 所在包,并扫描其中的注解--> <context:component-scan base-package="com.dang.controller"/> <!-- 静态资源(js、image等)的访问 --> <mvc:default-servlet-handler/> <!-- 开启注解 --> <mvc:annotation-driven/> <!--ViewResolver 视图解析器--> <!--用于支持Servlet、JSP视图解析--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/View/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
ps: bean标签中<property name="prefix" value="/WEB-INF/View/"/>对应我们原先新建的View文件,存放视图文件。
如遇报错,alt+enter 引入命名空间
11、在 src/main/java下新建com.dang.controller文件(用于存放springmvc的控制器文件--并对应mvc-dispatcher-servlet.xml中context便签的路径)
12、修改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>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
ps: servlet-name对应新建mvc-dispatcher-servlet.xml名字中-servlet前面的mvc-dispatcher
url-pattern为 / ,说明拦截所有请求交由后台控制器处理
13、在com.dang.controller下新建HomeController的java class文件,内容如下:
package com.dang.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by dang on 2017/1/4. */ @Controller @RequestMapping(value = "home") public class HomeController { @RequestMapping(value = "index", method = RequestMethod.GET) public String index(ModelMap modelMap) { modelMap.addAttribute("msg", "hello");//请页面传一个为msg变量的值 return "home/index";//返回路径为home文件夹下index.jsp文件 } }
@Controller:采用注解的方式,定义该类为处理请求的Controller类
@RequestMapping:用于定义一个请求映射,value为请求的url,method用以指定该请求类型,一般为get和post
在HomeController上面写@RequestMapping的值为home(说明经过这个控制器的头路径都为home),下面index的@RequestMapping为index,(
说明请求路径为home/index)
14、页面index文件
15、配置Tomcat,运行程序
点击菜单栏--Run-》Edit configurations
(1)Name上取一个名称,没配置tomcat需要点击configure配置
(2)选择Deployment选项卡,点击右下方绿色+号,选择artifacts-》选中带war exploded
16、运行程序-输入http://localhost:8080/home/index
ok 出现此界面说明程序配置成功 大功告成
end.项目总结构