如何在Intelli IDEA中优雅的创建SpringMVC项目
本文讲述如何在IntelliIDEA中,快速优雅的创建SpringMVC项目。
操作方法
(01)首先,我们选择新建,然后空项目,填入你想要的名字即可,如图所示。这里选择空项目是为了后续,我们可以扩展新的模块进去。正常情况下,新建完这个空项目,会自动弹出新增的模块的对话框,如图示。新建
(02)来到新增模块的界面,选择如图示的+,并在下啦列表中选择New Module,来到创建模块的界面。
(03)此处我们以新建maven模块为例。选择maven,勾选archetype,并从可选项中选择webapp,直接下一步。
(04)填写好对应的信息,一步到底即可。然后idea会自动创建一个基础工程目录出来。我们可以看到有pom文件,资源目录等。
(05)根据我个人的开发习惯,还要做一系列的调整。一,选择开发语言等级;二,创建Java根目录,这里将用来编写Java代码。
(06)接下来就是编码了。web.xml作为tomcat启动项目的入口,会自动创建。然后我们还要手动创建一个SpringMVC的xml文件。这里我直接从其他项目拷贝一个文件过来作为示例说明。不过在这之前,我们得先添加springmvc的依赖。<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version></dependency>注意,如果只是简单的SpringMVC项目,那么仅此一个依赖项就够了。如果涉及到aop orm 或者spring的其他特效,我们再按需要添加其他依赖。
(07)<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 静态资源默认servlet配置 1、加入对静态资源的处理:js,css,gif,png 2、允许使用"/"做整体映射 --> <mvc:default-servlet-handler/> <!-- 开启注解 --> <mvc:annotation-driven /> <context:annotation-config /> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="com.zjoin.story.admin.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 --> </list> </property> </bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/html/"/> <property name="suffix" value=".html"/> </bean> <!-- 开启AOP拦截 --> <!--<aop:aspectj-autoproxy proxy-target-class="true"/>--></beans>--------------------------------------------------------------------------------以上是配置全文,大家根据自己的实际情况修改包路径即可。
(08)<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>admin</display-name> <welcome-file-list> <welcome-file>/WEB-INF/html/index.html</welcome-file> </welcome-file-list> <!-- Spring和mybatis的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-*.xml</param-value> </context-param> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:springMvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- SpringMVC拦截设置 --> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 由SpringMVC拦截所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- SpringMVC拦截设置结束 --> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>--------------------------------------------------------------------------------这里有个小细节,可以注意到springMvc和我引用的其他spring文件命名格式不一样,这是为了单独加载SpringMVC文件,而不会出现重复加载的情况。
(09)到这里差不多就配置好了,接下去去编写你的Controller吧!