Java学习之路操作实战

IntelliJ IDEA创建SpringMVC+Maven项目

2016-03-10  本文已影响20539人  MaxZing
创建工程

选择空工程

选择空工程

输入工程名称,和工程存储位置

输入工程名称,选择位置

下一步

创建Maven模型,Web-APP

创建新Model 选择Model 写好Maven的俩参数,GroupId和ArtefactId,版本嘛,菜鸟忽略 选好你的Maven setting.xml文件,配置好你的Maven仓库!

写上你的Model的名称
选择finish就OK了
记得pom.xml文件要设为可编辑状态

在Model文件夹上,右键点击

Paste_Image.png 选择Spring框架支持

菜鸟请勾上Create empty spring-config.xml
如果你有本地Maven库的话,选择Set up library later
没有可以选择Download

项目结构变成了下面的样子

Paste_Image.png

修改工程结构(个人习惯)

这样滴

修改pom.xml

<dependencies>下加上节点

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

这样SpringMVC的jar包就引入完成了

注意一下WEB.XML的描述,最好参考一下Tomcat最新的WEB.XML头,因为JavaEE一直都在更新,所以版本号会渐渐过时

<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_2_5.xsd"
         version="2.5">
<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

配置扫包目录:
最好新建一个SpringConfig的XML配置文件,当然你也可以使用自动生成的Spring配置文件,当然,描述头一定要注意,否则就找不到正确的节点解析方法,下面虽然有些描述文件暂不需要,但是还是加上了

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.zing"></context:component-scan>

</beans>

现在可以写Controller 和 页面了运行也是没问题,不过没有精致的页面,只有自带的Hello World。

上一篇下一篇

猜你喜欢

热点阅读