使用InstelliJ IDEA创建Spring MVC应用程序
环境版本
Windows 8.1
IDE:InstelliJ IDEA 13
Spring:Spring 4.1.1 & Spring MVC 4.1.1
WebLogic 10.3.0
JDK:1.6
在前文中,我们创建的Web应用程序,其实就是基于Spring MVC的Web项目。
因为我们在创建时就选择了Spring 4.1.1和Spring MVC 4.1.1,IDEA会帮我们自动下载相关的Jar包,所以我们只需要完成一些配置。
(1)打开web.xml,编辑内容:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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> <!-- Log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>
其中需要注意,如果指定了mvc-dispatcher这个名称,那么则必须创建mvc-dispatcher开头的文件,用于配置Spring MVC。
(2)打开mvc-dispatcher-servlet.xml,编辑内容:
<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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描所有的controller -->
<context:component-scan base-package="com.hellomvc.controllers"/> <!-- 启动注解驱动SpringMVC功能 -->
<mvc:annotation-driven />
<!-- 处理静态资源 -->
<mvc:default-servlet-handler /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- tiles配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
</property>
<property name="order" value="1" />
</bean> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
其中需要注意,因为我们在项目中使用了Tiles框架,如果没有使用不用添加.
另外,<mvc:default-servlet-handler />,因为在web.xml中,我们将DispatcherServlet请求映射配置为"/",所以Spring MVC将捕获Web容器所有的请求,包括静态资源的请求,Spring MVC会将它们当成一个普通请求处理,因此找不到对应处理器将导致错误。采用<mvc:default-servlet-handler />后,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。
(3)我们在项目中使用Tiles框架,我们再来看看tiles.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions> <definition name="base.definition" template="/pages/shared/layout.jsp">
<put-attribute name="body" value="" />
</definition> <definition name="home.*" extends="base.definition">
<put-attribute name="body" value="/pages/{1}.jsp"/>
</definition> </tiles-definitions>
这里我们定义了一个基础模板"base.definition",对应的模板文件:/pages/shared/layout.jsp,其中定义了一个"body"属性.定义了一个通配模板"home.*"的模板名称,继承于"base.definition",其中的body属性,对应的模板文件:/pages/{1}.jsp,其中{1},根据通配符获得.
(4)再看看controller类文件

这里返回"home.index",就会被指向到tiles.xml配置的视图文件.
(5)视图文件,index.jsp

(6)最后,看看完整的目录结构:

(7)运行

使用InstelliJ IDEA创建Spring MVC应用程序的更多相关文章
- Eclipse下创建Spring MVC web程序--非maven版
首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...
- Eclipse下创建Spring MVC web程序--maven版
1. 创建一个maven工程: File->New->Other... 2. 创建完成后的结构如下: 3. 配置pom.xml文件,添加spring-webmvc依赖项 <pro ...
- Intellij IDEA创建spring MVC项目
相信各位未来的Java工程师已经接触到了spring MVC这个框架的强大之处,看了很多的教程,都是eclipse的,在intellij IDEA这个强大的工具面前居然不能很顺畅的,今天我就带领大家用 ...
- IntelliJ idea创建Spring MVC的Maven项目
参考:http://my.oschina.net/gaussik/blog/385697?fromerr=Pie9IlFV 创建Maven Web项目 菜单File->New Project可进 ...
- 用maven创建Spring MVC项目
用maven创建Spring MVC项目 mvn archetype:generate -DgroupId=fry-arthur -DartifactId=spring-mvc-study -Darc ...
- 使用idea创建spring mvc项目图文教程
使用idea创建spring mvc项目图文教程 前言: 使用惯了eclipse的朋友,如果刚换成了idea或许有些不习惯.但是使用idea之后,就会love上idea了.本文将通过图文讲解怎么通过i ...
- 在 Spring MVC 应用程序中使用 WebMvcTest 注释有什么用处?
在测试目标只关注 Spring MVC 组件的情况下,WebMvcTest 注释用于单元测试 Spring MVC 应用程序.在上面显示的快照中,我们只想启动 ToTestController. 执行 ...
- 使用eclipse创建spring mvc web工程并部署
创建maven工程
- IDEA 通过Maven创建Spring MVC项目搭建
概述 本篇随笔主要记录内容如下: 1.通过Maven创建基于Spring Framework类库的MVC项目,免去了繁琐的XML配置: 2.在Idea里面配置Tomcat的测试启动项: Maven创建 ...
随机推荐
- find命令扩展
1.1 方法一 |xargs 通过|xargs将前面命令的执行结果传给后面. [root@znix ~]# find /clsn/ -type f -name "*.sh" |x ...
- applicationContext-solr.xml
一.动态切换单机和集群 spring-solr 的配置 <!-- 单机版 solrj --> <bean id = "httpSolrServer" class= ...
- LoadRunner性能测试-下载文件脚本
Action() { intflen; //定义一个整型变量保存获得文件的大小 longfiledes; //保存文件句柄 charfile[]="\0"; //保存文件路径及文件 ...
- 【iOS开发系列】XIB IBOutlets use strong or weak ?
有人问.在ARC下,IBOutlets究竟应该定义成strong 还是 weak ?支持这个答案的人最多.答案仅是摘自官方文档的一个片段: From a practical perspective, ...
- BZOJ 2124: 等差子序列 线段树维护hash
2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- Ubuntu 16.04 安装 Wireshark分析tcpdump的pcap包——sudo apt install wireshark-qt
tcpdump 的抓包保存到文件的命令参数是-w xxx.cap 抓eth1的包 tcpdump -i eth1 -w /tmp/xxx.cap 抓 192.168.1.123的包 tc ...
- 机器学习案例学习【每周一例】之 Titanic: Machine Learning from Disaster
下面一文章就总结几点关键: 1.要学会观察,尤其是输入数据的特征提取时,看各输入数据和输出的关系,用绘图看! 2.训练后,看测试数据和训练数据误差,确定是否过拟合还是欠拟合: 3.欠拟合的话,说明模 ...
- 高斯混合模型Gaussian Mixture Model (GMM)——通过增加 Model 的个数,我们可以任意地逼近任何连续的概率密分布
从几何上讲,单高斯分布模型在二维空间应该近似于椭圆,在三维空间上近似于椭球.遗憾的是在很多分类问题中,属于同一类别的样本点并不满足“椭圆”分布的特性.这就引入了高斯混合模型.——可以认为是基本假设! ...
- lightoj--1245--Harmonic Number (II)(数学推导)
Harmonic Number (II) Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...