相关代码:http://pan.baidu.com/s/1jIBUr1G

目标: 新建一个名为testSpringMvc的spring MVC工程,maven管理jar包;

1、新建maven管理的web工程

  eclipse : new project ---> maven project

---> next

  

  ---> next:

  

  ---> next:

  

  Group Id:一般填写包名前缀,公司包名,大项目包名

  Artifact Id:一般填写项目包名

  GroupID 和Artifact ID共同组成代码的包路径。

  --->  finish:

  结束后目录结构如下:

  

  工程新建后报错(jar包依赖环境不同,可能有些会报错有些不会):

  

  修改pom加上相关依赖解决错误:

  

  

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

  run as ----> maven install 测试一下啊有没有编译报错。

  run as ---->run on server 运行工程看看有没有报错,或者run install后运行tomcat 指定工程目录为target/testSpringMVC目录启动工程测试

  没有问题进行下一步。

2、给工程添加spring MVC

 2.1  添加spring MVC相关jar包:

  

<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.xw</groupId>
<artifactId>textAna</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>textAna Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 属性:spring 版本 -->
<properties>
<springVersion>3.2.5.RELEASE</springVersion>
</properties>
<dependencies>
......
<!--spring MVC相关jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springVersion}</version>
</dependency>
......
</dependencies>
</project>

  2.2 修改web.xml 添加spring MVC支持

  

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Spring MVC Test</display-name>
<!-- 配置spring 配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-*.xml</param-value>
</context-param>
<!-- java beans 内存泄漏监听类,防止内存泄漏 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 核心配置:spring MVC DispatcherServlet -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-mvc.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 过滤器,解决页面参数传递时中文乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

添加后发现报错如下:

  实测删除1~3行的格式校验声明错误消失,且工程运行没有问题。

  2.3  配置spring mvc配置文件

  如下图,新建config/spring/spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">    <!-- 开启注解扫描功能,指定扫描的包名,不配置注解无效 -->
<context:component-scan base-package="com.sunline.*" />
<!-- 配置读取外部配置文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 数据源参数配置,使用c3p0包提供的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 此行语句使得resource autowired 等四个注解可以使用 -->
<context:annotation-config /> <!-- 实例化jdbcTemplate,同时注入数据源 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" /> <!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 避免返回中文乱码问题 -->
<mvc:annotation-driven >
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>

  从上面的配置看出,我们还需要如下几个配置:

    -- classpath:config.properties 即WEB-INF/config.properties,由于是maven工程,可以将该文件放到src/main/resources目录下即可,工程编译运行时会自动拷贝到target的WEB-INF中:

    config.properties:

  ps:因为公司项目上使用的是星环的数据库,所以配置的driverClass、url、username、password等都如下,其他项目请根据实际情况设置,只要能获得数据就算成功。

#jdbc config
jdbc.driverClass=org.apache.hive.jdbc.HiveDriver
jdbc.username=easuser
jdbc.password=Eas@20170417
#DAT
jdbc.url=jdbc:hive2://10.80.2.102:10000,10.80.2.103:10000/easdb

  --  因为工程使用到json 和c3p0包,pom.xml需要添加如下配置:

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

  至此,spring MVC的配置完成,接下来编写代码测试

3、测试工程

  在src/main 下面新建com.sunline.testSpringMvc.controller、com.sunline.testSpringMvc.service、com.sunline.testSpringMvc.dao  三个包,分别在包中新建TestController.java、TestService.java、TestDao.java文件。新建后如下图:

  

  TestController.java:

package com.sunline.testSpingMvc.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sunline.testSpingMvc.service.TestService; @Controller
@RequestMapping("/Test")
public class TestController {
@Autowired
TestService testService; @RequestMapping("/getResult")
@ResponseBody
public String getResult(String userId,HttpServletRequest request){
String result = "";
result = testService.getResult(userId);
return result;
}
}

  TestService.java:

  

package com.sunline.testSpingMvc.service;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sunline.testSpingMvc.dao.TestDao;
import net.sf.json.JSONArray;
@Service("TestService")
public class TestService {
@Autowired
TestDao testDao; /**
* 取数据、业务处理逻辑
* @param batchId
* @return 结果字符串
*/
public String getResult(String userId) {
//取数据
List<Map<String, Object>> list= testDao.getResult(userId);
//处理数据逻辑,此处直接返回为jsonarray对象字符串
return JSONArray.fromObject(list).toString();
}
}

  TestDao.java:

package com.sunline.testSpingMvc.dao;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; @Repository("TextAnaDao")
public class TestDao {
@Autowired //spring管理bean
private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 从eas_text_ana_file表获取数据
* @param userId
* @return
*/
public List<Map<String, Object>> getResult(String userId) {
String sql = "select * from eas_text_ana_file";
return this.jdbcTemplate.queryForList(sql);
}
}

  运行工程,浏览器输入:http://localhost:8080/testSpringMvc/test/getResult  回车,浏览器页面成功得到数据表记录返回的json字符串。表示成功。

PS: 实际测试请修改相关的数据库配置,以实际使用的数据库为准。

  既然是web工程,当然也可以编写页面来访问后台获取数据,如使用ajax请求访问http://localhost:8080/testSpringMvc/test/getResult获取数据等方法

从零开始新建一个Maven 、springMVC工程的更多相关文章

  1. Maven:如何在eclipse里新建一个Maven的java项目和web项目

    如何在eclipse里新建一个Maven的java项目和web项目: 一:java项目 New-->Other-->Maven 右击项目-->properties,修改以下文件: ① ...

  2. 使用IDEA创建一个Maven Web工程:无法创建Java Class文件

    今天用IDEA新建了一个maven web工程,项目目录是这样的: 在新创建一个Java class 文件时,却没有Java class功能,无法创建,如图: 解决方案: 选择 File——>P ...

  3. idea 如何新建一个Maven项目并且写第一个servlet

    使用idea已经有段时间了,但是一直没有自己亲自新建一个项目,从头开始写一个Servlet,今天就来学习一下,并且记一个笔记. 一. 1.首先,打开idea new-->Project 2.选择 ...

  4. 如何在idea里面新建一个maven项目,然后在这个maven项目里创建多个子模块

    如何在idea里面配置maven我这里就不多说了 先新建一个maven项目作为总的管理项目 不用勾选什么,直接下一步 这样子一个普通的maven项目就创建成功了. 因为这个项目是用来管理多个子模块的, ...

  5. 【Java_SSM】(二)使用eclipse创建一个Maven web工程

    这篇博文我们介绍一下如何利用eclipse创件一个maven web工程. (1)File--New--Other--Maven--Maven project 此处我们快速创建一个maven工程 点击 ...

  6. ecplise maven springmvc工程搭建

    转载自:https://www.cnblogs.com/crazybirds/p/4643497.html 内网上网代理配置: 第一步:新建maven项目,选择Maven Project,如图1.   ...

  7. 利用eclipse新建一个maven项目步骤:

    1.打开eclipse,左键点击左上角File,选中New,左键点击选中Maven Project,出现下面界面: 2.把打钩的去掉,选择自己项目所在的工作空间,如下图,我建在我的工作空间worksp ...

  8. IDEA创建maven web工程

    一.新建一个maven web工程 step1 File --> New --> Project step2 按下图步骤操作 step3 填写项目信息 step4 选择本地的maven安装 ...

  9. Eclipse中一个Maven工程的目录结构

    在之前的javaSE开发中,没有很关注Eclipse工程目录下的环境,总是看见一个src就点进去新建一个包再写一个class.以后的日子中也没有机会注意到一个工程到底是怎么组织的这种问题,跟不要说自己 ...

随机推荐

  1. WebConfig配置详解大全

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  2. You don't have permission to access

    局域网内配置Discuz,web端访问server端,出现以下反馈: Forbidden You don't have permission to access / on this server. 网 ...

  3. 使用 Visual Studio 将 ASP.NET Web 应用部署到 Azure

    原文地址:https://www.azure.cn/zh-cn/documentation/articles/web-sites-dotnet-get-started 配置新的 Web 项目 下一步是 ...

  4. Andrew Ng机器学习公开课笔记 -- Logistic Regression

    网易公开课,第3,4课 notes,http://cs229.stanford.edu/notes/cs229-notes1.pdf 前面讨论了线性回归问题, 符合高斯分布,使用最小二乘来作为损失函数 ...

  5. Django - Jsonp、CORS

    一.同源策略 https://www.cnblogs.com/yuanchenqi/articles/7638956.html 同源策略(Same origin policy)是一种约定,它是浏览器最 ...

  6. UML Diagrams Using Graphviz Dot

    Introduction Background This article is about using the dot tool from the Graphviz package to automa ...

  7. 初识MVCSharp

    MVCSharp其实是MVP模式 主要内容 ITask IView IController

  8. ppt 文字按笔画进入。

    1:在word里面写一个字(艺术字或非艺术字都可),字体格式设置为  楷体_gb23212, 没有则下载安装,http://www.cnblogs.com/liyafei/p/8747992.html ...

  9. Spark的Java API例子详解

    package com.hand.study; import scala.Tuple2; import org.apache.spark.SparkConf; import org.apache.sp ...

  10. 29张截图-全新安装CentOS7.5-超详细!

    目录 全新安装CentOS7.5 配置虚拟机 调整网卡名称 配置时区,分区,关闭安全工具 配置网络参数 配置root账户密码 参考链接 全新安装CentOS7.5 可以到这里下载镜像https://m ...