intelli J IDEA 创建maven项目及部署
1.打开intelli J IDEA
2.File-->New-->Projectc
3.选择maven ,右侧打勾 ,选择模板,里面有两个webapp,要选择正确的
4.创建项目
5选择maven的的安装目录,还有setting.xml文件位置
6创建的项目名称
7.下面只有pom文件,等待下载
8下载完成
9.在main下面创建Java文件夹,且将其改为Source Root
10.在pom.xml文件中加入注解,方法:
(1)百度输入maven,点击进入
(2)输入spring webmvc
(3)点击spring webmvc,选择版本,复制依赖
(4)复制到pom.xml文件之后,将会自动下载,如图已经添加到本地仓库中
(5)同理,在网站里复制servlet和jstl
(6)在pom.xml文件中配置Apach,将其添加到build下面,1.8为自己的jdk版本
11.配置web.xml文件:
(1)头文件:1.配置spring-mvc.xml前端控制器2,配置编码过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" 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_3_1.xsd"> <display-name>Archetype Created Web Application</display-name>
<filter> <description>请求和应答字符编码过滤器</description> <filter-name>encoding-filter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encoding-filter</filter-name> <servlet-name>spring-mvc</servlet-name> </filter-mapping> <servlet> <description>spring-mvc的前端控制器</description> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app>
(2)在java文件夹下面创建包com.test.controller,在包下面创建Test类
(3)在WEB-INF下面创建views文件夹,在views里面创建一个index.jsp
(4)在resources下面创建spring-mvc.xml文件:1.在里面添加注解,视图解析器,扫描控制器
<?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"> <!--使用注解来进行请求映射--> <mvc:annotation-driven/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--重定向时是否添加上下文路径--> <property name="redirectContextRelative" value="true"></property> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--扫描所有的控制器--> <context:component-scan base-package="com.test.controller"></context:component-scan> </beans>
12.test类:
@Controller public class Test { @RequestMapping("/test.do") public String test(){ System.out.println("我进来了"); return "index"; } }
13.配置Tomcat(略)
14.发布右上角或者左下角,绿色的三角形
总结:我遇到的问题:
1.创建maven工程时若只有pom.xml文件或者找不到本地仓库,可能是intelli J IDEA配置maven出错(setting.xml里面的路径或者其它属性错误)。
2.发布时,视图解析器中的配置不能出错,否则找不到路径。
3.spring-mvc.xml文件中可能缺少头某些文件