[转载]架构指南 : Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4

简介:

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

 

b)         下一步

 

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

 

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

 

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

 

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

 

3、 修改项目基本设置

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

 

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

 

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

 

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

 

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

 

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

 

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

 

  

5、 简单Spring mvc的配置

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

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

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
< 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文件,最终修改如如下内容:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<? 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,内容如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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" />已经指定了路径

 

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

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package  com.springstudy.controller;
 
  
 
import  org.springframework.stereotype.Controller;
 
import  org.springframework.web.bind.annotation.RequestMapping;
 
import  org.springframework.web.servlet.ModelAndView;
 
  
 
@Controller
 
public  class  HelloController {
 
  
 
          @RequestMapping ( "/hello" )
 
          public  ModelAndView hello(){
 
                    ModelAndView mv = new  ModelAndView();
 
                    mv.addObject( "spring" "spring mvc" );
 
                    mv.setViewName( "hello" );
 
                    return  mv;
 
          }
 
}

  

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

1
2
3
4
5
6
7
8
9
10
<! 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

 

d)         启动tomcat

 

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

 

 

本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/5473744.html,如需转载请自行联系原作者
相关文章
|
3月前
|
IDE Java 数据库连接
解决Java环境中无法识别org.mybatis.spring.annotation.MapperScan的问题。
祝你好运,在这场MyBatis的魔法冒险中获得胜利!记住,魔法书(官方文档)永远是你最好的朋友。
257 18
|
2月前
|
Java 数据库连接 API
Java 8 + 特性及 Spring Boot 与 Hibernate 等最新技术的实操内容详解
本内容涵盖Java 8+核心语法、Spring Boot与Hibernate实操,按考试考点分类整理,含技术详解与代码示例,助力掌握最新Java技术与应用。
98 2
|
2月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
242 0
|
3月前
|
Java 数据库连接 API
Java 对象模型现代化实践 基于 Spring Boot 与 MyBatis Plus 的实现方案深度解析
本文介绍了基于Spring Boot与MyBatis-Plus的Java对象模型现代化实践方案。采用Spring Boot 3.1.2作为基础框架,结合MyBatis-Plus 3.5.3.1进行数据访问层实现,使用Lombok简化PO对象,MapStruct处理对象转换。文章详细讲解了数据库设计、PO对象实现、DAO层构建、业务逻辑封装以及DTO/VO转换等核心环节,提供了一个完整的现代化Java对象模型实现案例。通过分层设计和对象转换,实现了业务逻辑与数据访问的解耦,提高了代码的可维护性和扩展性。
155 1
|
3月前
|
SQL Java 数据库
解决Java Spring Boot应用中MyBatis-Plus查询问题的策略。
保持技能更新是侦探的重要素质。定期回顾最佳实践和新技术。比如,定期查看MyBatis-Plus的更新和社区的最佳做法,这样才能不断提升查询效率和性能。
149 1
|
3月前
|
Java 调度 流计算
基于Java 17 + Spring Boot 3.2 + Flink 1.18的智慧实验室管理系统核心代码
这是一套基于Java 17、Spring Boot 3.2和Flink 1.18开发的智慧实验室管理系统核心代码。系统涵盖多协议设备接入(支持OPC UA、MQTT等12种工业协议)、实时异常检测(Flink流处理引擎实现设备状态监控)、强化学习调度(Q-Learning算法优化资源分配)、三维可视化(JavaFX与WebGL渲染实验室空间)、微服务架构(Spring Cloud构建分布式体系)及数据湖建设(Spark构建实验室数据仓库)。实际应用中,该系统显著提升了设备调度效率(响应时间从46分钟降至9秒)、设备利用率(从41%提升至89%),并大幅减少实验准备时间和维护成本。
250 0
|
3月前
|
Java API 微服务
Java 21 与 Spring Boot 3.2 微服务开发从入门到精通实操指南
《Java 21与Spring Boot 3.2微服务开发实践》摘要: 本文基于Java 21和Spring Boot 3.2最新特性,通过完整代码示例展示了微服务开发全流程。主要内容包括:1) 使用Spring Initializr初始化项目,集成Web、JPA、H2等组件;2) 配置虚拟线程支持高并发;3) 采用记录类优化DTO设计;4) 实现JPA Repository与Stream API数据访问;5) 服务层整合虚拟线程异步处理和结构化并发;6) 构建RESTful API并使用Springdoc生成文档。文中特别演示了虚拟线程配置(@Async)和StructuredTaskSco
371 0
|
3月前
|
监控 安全 Java
Java 开发中基于 Spring Boot 3.2 框架集成 MQTT 5.0 协议实现消息推送与订阅功能的技术方案解析
本文介绍基于Spring Boot 3.2集成MQTT 5.0的消息推送与订阅技术方案,涵盖核心技术栈选型(Spring Boot、Eclipse Paho、HiveMQ)、项目搭建与配置、消息发布与订阅服务实现,以及在智能家居控制系统中的应用实例。同时,详细探讨了安全增强(TLS/SSL)、性能优化(异步处理与背压控制)、测试监控及生产环境部署方案,为构建高可用、高性能的消息通信系统提供全面指导。附资源下载链接:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)。
521 0

推荐镜像

更多