本人环境OS X,使用Intellij IDEA
基于maven来创建webapp骨架,使用maven来管理项目有很多好处,具体的话请自行百度
首先确保maven已安装
终端下执行
mvn -v
1.打开IDEA,FIle- New project…
然后填写GroupId、ArtifactId,Version
直接next
完善项目目录结构
具体可参考官方Maven标准目录布局指南
src/main/java:开发目录
在java目录右键选择Mark Directory As里的Sources Root
此时java目录颜色会变成浅蓝色,只有将文件夹标记为Sources Root后,才能通过IDEA右键该文件夹后通过菜单直接在其下创建package和class。
src/main/resources:资源目录
资源目录下的文件是会被编译到输出目录下的。跟上面标记src/main/java目录的方法类似,若src/main/resources目录尚未被标记为Resources Root则需要手动将其标记为Resources Root。不过好像这个文件夹默认就已经被标注为Resources Root了。
这里加入Spring框架的依赖
编辑pom.xml文件,添加spring相关依赖
dependency groupId javax.servlet /groupId artifactId javax.servlet-api /artifactId version 3.1.0 /version /dependency !--spring相关包start-- dependency groupId org.springframework /groupId artifactId spring-web /artifactId version 4.3.1.RELEASE /version /dependency dependency groupId org.springframework /groupId artifactId spring-webmvc /artifactId version 4.3.1.RELEASE /version /dependency !--spring相关包end-- !--JSP标准标签库-- dependency groupId javax.servlet /groupId artifactId jstl /artifactId version 1.2 /version /dependency
需要注意的是spring-web和spring-webmvc的版本要一致,不然的话可能会出现问题
后续需要其他jar包(如mysql,log4j等等),再手动添加,Jar包搜索
添加依赖项后,Intellij IDEA会自动下载jar并放在里面External Libraries
如图所示正在下载中。。。
下载完成后即可看到External Libraries下多了相应的jar包
如果修改pom.xml后依赖不生效呢?
Intellij IDEA菜单栏 View-》Tool Windows-》Maven Projects
在Maven Projects中点击第一个按钮Reimport即可生效
编辑web.xml,内容如下
?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_3_1.xsd version= 3.1 !--spring监听器-- context-param param-name contextConfigLocation /param-name param-value /WEB-INF/applicationContext.xml /param-value /context-param listener listener-class org.springframework.web.context.ContextLoaderListener /listener-class /listener servlet servlet-name dispatcher /servlet-name servlet-class org.springframework.web.servlet.DispatcherServlet /servlet-class load-on-startup 1 /load-on-startup /servlet servlet-mapping servlet-name dispatcher /servlet-name !-- url-pattern *.form /url-pattern -- url-pattern / /url-pattern /servlet-mapping /web-app
在WEB-INF下新增applicationContext.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 /beans
再新增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:context= http://www.springframework.org/schema/context xmlns:mvc= http://www.springframework.org/schema/mvc 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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd context:annotation-config/ mvc:annotation-driven/ !--控制器所在的包-- context:component-scan base-package= com.mycontroller / !-- 静态资源(js、image等)的访问 -- mvc:default-servlet-handler/ !--ViewResolver 视图解析器-- !--用于支持Servlet、JSP视图解析-- bean id= jspViewResolver >java文件夹右键新增Package,名为com.mycontroller,接着新增HomeController.java
HomeController.java内容如下package com.mycontroller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping(value = /home , method = RequestMethod.GET)public class HomeController { @RequestMapping(value = /index , method = RequestMethod.GET) public String index(Model m) { return index //视图名为index在WEB-INF/views下面新增index.jsp文件
%@ page contentType= text/html;charset=UTF-8 language= java % html head title Title /title /head body 这是home控制器的index视图 /body /html
设置tomcat先设置Deployment选项卡
再设置Server
编译运行,再浏览器输入http://localhost:8080/home/index 即可看到效果
本文链接: http://vastspring.immuno-online.com/view-744613.html