简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4

摘要:
在常规项目目录中,javaResources目录下还有src/main/test/resourcesourcefolder。首先删除缺少的项目,然后重新创建它们。直接创建缺少的项目。

转载请注明出处:http://www.cnblogs.com/mybest/p/4265872.html

1、 环境配置

a) Java 1.7

b) Eclipse luna

c) Maven3.2.5

d) Spring 4.1.4

2、 创建maven工程

a) 打开eclipse,file->new->project->Maven->Maven Project

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第1张

b) 下一步

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第2张

c) 选择创建的工程为webapp,下一步

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第3张

d) 填写项目的group id和artifact id。一般情况下,group id写域名的倒序,artifact id写项目名称即可。最后点完成。

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第4张

e) 最初建好后,项目目录结构如下

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第5张

f) 一般的项目目录中,在java Resources目录下,还有src/main/java,src/main/test/java,src/main/test/resources这三个source folder,需要手动创建。在下面的步骤中会讲到如何补齐这三个目录。

3、 修改项目基本设置

a) 右键此项目名称->Properties->Java Build path,点击source标签。

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第6张

b) 提示 hello/src/main/java (missing)和hello/src/test/java (missing)。一般的项目目录中,在java Resources目录下,还会有src/main/test/resources这个source folder。将missing的先删除,再重新创建,缺少的直接创建。点右键操作按键进行删除和添加。

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第7张

c) 修改完整,效果如下图

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第8张

d) 接下来再修改libraries的配置,jre使用1.7版本。选中JRE System Library->edit ,更换版本。

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第9张

e) 再修改一下order and export里的配置,主要是调整这四个目录的显示顺序,调为自己喜欢的顺序即可

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第10张

f) 接下来再修改project facets,先将java修改为1.7。

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第11张

Dynamic Web Module无法在这里直接修改为3.0,需要打开工程目录下有一个.settings文件夹,打开org.eclipse.wst.common.project.facet.core.xml,做如下修改:

<installed facet="jst.web"version="3.0"/>

重启eclipe就可以看到更改生效了。

4、 Eclipse中maven的配置

a) window->properties->maven,勾选 download repository index updates on startup

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第12张

5、 简单Spring mvc的配置

a) 打开项目中的pom.xml文件,并点击Dependencies标签,点击add添加新的依赖

b) 如果知道依赖的group id和artifact id,可以直接填写,如果不清楚,可以输入关键字进行查询,或是到http://search.maven.org网站查询

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第13张

c) 需要添加的依赖有:spring-webmvc,版本为4.1.4. RELEASE。完整的POM.XML文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springstudy</groupId>

  <artifactId>study</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>study Maven Webapp</name>

  <url>http://maven.apache.org</url>

  <properties>

                   <spring.version>4.1.4.RELEASE</spring.version>

         </properties>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>${spring.version}</version>

    </dependency>

  </dependencies>

  <build>

    <finalName>study</finalName>

  </build>

</project>

d) 打开src/main/webapp/WEB-INF/web.xml文件,最终修改如如下内容:

<?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"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="study"version="2.5">

         <display-name>Archetype Created Web Application</display-name>

         <description>sprintMVC环境搭建</description>

         <!--加载Spring配置文件 -->

         <context-param>

                   <param-name>contextConfigLocation</param-name>

                   <param-value>classpath:/configs/spring-*.xml</param-value>

         </context-param>

         <!--Spring监听 -->

         <listener>

                   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

         </listener>

         <!--Spring MVC配置 -->

         <servlet>

                   <servlet-name>Dispatcher</servlet-name>

                   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

                   <!--自定义spring mvc的配置文件名称和路径 -->

                   <init-param>

                            <param-name>contextConfigLocation</param-name>

                            <param-value>classpath:configs/spring-servlet.xml</param-value>

                   </init-param>

                   <load-on-startup>1</load-on-startup>

         </servlet>

         <!--spring mvc 请求后缀 -->

         <servlet-mapping>

                   <servlet-name>Dispatcher</servlet-name>

                   <url-pattern>/</url-pattern>

         </servlet-mapping>

         <welcome-file-list>

                   <welcome-file>index.jsp</welcome-file>

         </welcome-file-list>

</web-app>

e) 在Java Resources/scr/main/resources目录下,创建configs文件夹,以便存放在web.xml中声明的配置路径

f) 在Java Resources/scr/main/resources/configs目录下,创建spring-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:jee="http://www.springframework.org/schema/jee"
         xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"
         xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"
         xsi:schemaLocation="http://www.springframework.org/schema/beans

                                           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

                                                   http://www.springframework.org/schema/context

                                                   http://www.springframework.org/schema/context/spring-context-4.0.xsd

                                                   http://www.springframework.org/schema/jee

                                                        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd

                                                        http://www.springframework.org/schema/mvc

                                                   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

                                                   http://www.springframework.org/schema/util 

                                                   http://www.springframework.org/schema/util/spring-util-4.1.xsd">

 

        

         <context:annotation-config/>

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

         <mvc:annotation-driven />

        

         <mvc:resources mapping="/styles/**"location="/styles/" />

         <mvc:resources mapping="/scripts/**"location="/scripts/" />

         <mvc:resources mapping="/images/**"location="/images/" />

 

         <bean

                   class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                   <property name="prefix"value="/WEB-INF/views/" />

                   <property name="suffix"value=".jsp" />

         </bean>

</beans>

g) 创建controller包,在spring-servlet.xml文件中,<context:component-scan base-package="com.springstudy.controller" />已经指定了路径

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第14张

h) 在src/main/webapp/WEB-INF目录下,创建views文件,在spring-servlet.xml文件中,<property name="prefix" value="/WEB-INF/views/" />已指定了视图文件路径

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第15张

i) 创建第一个controller文件HelloController.java,完整的文件内容如下:

packagecom.springstudy.controller;

 

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.servlet.ModelAndView;

 

@Controller

public classHelloController {

 

         @RequestMapping("/hello")

         publicModelAndView hello(){

                   ModelAndView mv =newModelAndView();

                   mv.addObject("spring", "spring mvc");

                   mv.setViewName("hello");

                   returnmv;

         }

}

j) 添加src/main/webapp/WEB-INF/views/hello.jsp文件,内容如下:

<!DOCTYPE html>

<html>

         <head>

                   <meta charset="utf-8">

                   <title>sprint hello</title>

         </head>

         <body>hello ${spring}!

         </body>

</html>

6、 将项目发布到tomcat

a) 在eclipse中添加tomcat 7

b) 在tomcat添加完成后,双击,设置overview选项卡中Server Locations的设置。

i. 将 Use Tomcat installation(takes control of Tomcat installation)选中

ii. 将Deploy path的内容改为:webapps

iii. 保存

c) 右键tomcat,Add and Remove… ,添加study

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第16张

d) 启动tomcat

e) 浏览器打开http://localhost:8080/study/hello,访问成功!如下图

简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4第17张

免责声明:文章转载自《简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇盘姬工具箱WV1.10ssh免密码认证下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Netty通过心跳保持长链接

 Netty自带心跳检测功能,IdleStateHandler,客户端在写空闲时主动发起心跳请求,服务器接受到心跳请求后给出一个心跳响应。当客户端在一定时间范围内不能够给出响应则断开链接。 Java代码   public class NettyClient {       public void connect(String remoteServer...

前端js上传文件 到后端接收文件

下面是前端js代码: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>File upload</title> </head>...

配置Struts应用解读Struts应用的两大配置文件

1.引言 关于Struts项目,在以前的文章中我已经做过一些介绍,在此不再赘述。写本文,旨在从纯Struts应用配置的角度深入剖析Struts配置文件在Web应用中的所扮演的角色及其工作机制,以加深对Struts内幕的理解和认知。以期在实际开发项目中灵活应用,提高分析和解决实际问题的能力。 2.进入主题 Struts应用采用两个基于XML的配置文件来配置应用...

Java Servlet 中文API说明

JavaServlet中文API说明 这份文档描述了Java Servlet API的最新版本2.1版。所以,这本书对于Servlet的开发者及servlet引擎的开发者同样适用。 Java Servlet API的组成 Java Servlet API由两个软件包组成:一个是对应HTTP的软件包,另一个是不对应HTTP的通用的软件包。这两个软件包的同时...

SpringMVC上传视频等大文件

前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方式来学习文件上传的原理与实践。该博客重在实践。 一. Http协议原理简介 HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。它于1990年提出...

shiro框架 4种授权方式 说明

1. shiro的配置文件(applicationContext-shiro.xml)中使用filterChain过滤url的方式 详细配置看注释 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/bean...