一、创建项目

1、打开idea,file--new--project

2、按照步骤①②③④操作

3、输入包名,并点击下一步

4、选择下载包的maven的setting.xml配置路径和包的存放地,然后点击next

5、确认最终的项目存放目录和项目名,点击finish

二、项目配置

1、在webapp/WEB-INF/web.xml里面配置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="WebApp_ID" version="2.5">
<display-name>demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.servlet.DispatcherServlet</listener-class>
</listener>
<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:root.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

注意:classpath加载的东西是随着项目启动而加载的,无需加载的则不需要配置

3、resources/root.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 注解、扫包、读取配置文件内容 -->
<import resource="spring-mvc.xml" />
<import resource="classpath:/spring/base-context.xml"/>
</beans>

注意:import是要导入的一些xml文件,此处导入了sprin-mvc.xml(mvc的web文件)文件和base-context.xml文件(基础的配置文件)

4、resources/spring-mvc.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 这里是扫描controller-->
<context:component-scan base-package="com.test.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
</beans>

这里是扫描controller层的代码目录,在base-package下,其他的配置一般不动

5、resources/spring/base-context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入定义/props/*.properties配置文件 -->
<context:property-placeholder location="classpath:/props/*.properties" ignore-unresolvable="true"/>
<!--<context:property-placeholder location="classpath:/data/*" ignore-unresolvable="true"/>--> <!-- spring 扫描所有注解-->
<context:component-scan base-package="com.test">
<!-- spring 排除@Controller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> </beans>

注意:这里是扫描除controller外的其他代码随着服务启动而初始化的配置项。如service和resources下的一些静态文件等

6、代码分层中的controller添加对外的访问路径、service层实现具体的业务逻辑、dao层是对于数据库操作的接口层(对应还有service下的mapper映射以及xml映射,若没有数据库操作则无需这些)、model层对应数据库的实体类层(也可使用bean)、util层对应的一些工具类层、enums层对应的是一些枚举映射(以上是基本的分层设计)

package com.test.controller;

import com.test.services.impl.CommonServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controller
@RequestMapping(value = "/common")
public class TestController { @ResponseBody
@CrossOrigin
@RequestMapping(value ="/getSMSRecord.html",method = RequestMethod.GET,produces = "text/html;charset=utf8")
public String test(HttpServletRequest req, HttpServletResponse res){
String mobile = req.getParameter("mobile");
String env = req.getParameter("env");
return "success";
}
}

注意:以上是controller层的代码示例;@Controller标识这个类是对外的服务的(必加);@RequestMapping标识对外映射的路径(必加,值自定义);@ResponseBody表示这个方法是对外的方法(关于spring的一些注解可百度自行搜索)

以上就是spring-mvc的简单项目配置(连接数据库除外//todo),若还想要本地调试,则还需要配置本地tomcat启动

三、配置tomcat启动本地服务

1、首先下载tomcat服务器,至少是8.0以上,并解压

2、从以下入口编辑启动配置

3、按照以下顺序添加tomcat本地服务器

4、再次点击第三步的编辑配置,然后选中刚才的tomcat,会出现下面的页面,并配置相关的参数

5、点击上一步的第③步的“+”号会出现以下画面,选中红框的那个

6、勾选中出现的额war包,并点击ok

7、最后点击页面右下角的apply,然后就能本地运行服务了。

注意:index.xml里面的内容是主页的内容,可自定义设置任何值。

注意2:没出现第六步的war包需要手动通过maven编译器打一个war包(需要依赖pom的一些配置参数,可自行百度//todo)

【idea&spring mvc】搭建简易的spring mvc项目(基于maven)!的更多相关文章

  1. 一步一步深入spring(1)--搭建和测试spring的开发环境

    1.引用jar包 到spring的网站上下载spring的jar包(本文是2.5.6),解压缩后找到 使用spring必须引用的jar包 spring.jar  commons-logging.jar ...

  2. ActiveMQ JMS 项目 基于 Maven 搭建 部署

    JAVA版本: IntellJ IDEA 版本: IntelliJ IDEA 2017.2Build #IU-172.3317.76, built on July 15, 2017Licensed t ...

  3. ASP.NET Core ASP.NET Core+MVC搭建及部署

    ASP.NET Core+MVC搭建及部署 一.创建项目: 1.选择ASP.NET Core Web Application(.NET Core) 注意框架 2.选择Web Application: ...

  4. Spring MVC 项目搭建 -3- 快速 添加 spring security

    Spring MVC 项目搭建 -3- 快速 添加 spring security 1.添加 spring-sample-security.xml <!-- 简单的安全检验实现 --> & ...

  5. 基于Maven的Spring + Spring MVC + Mybatis的环境搭建

    基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...

  6. Spring MVC 搭建web项目示例

    环境为Eclipse 1:新建Dynamic web project  : springMvcDemo 2:下载spring的jar包,把jar包复制到WEB-INF/lib目录下 3.添加配置文件w ...

  7. 25、ASP.NET MVC入门到精通——Spring.net-业务层仓储

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架. 1.假设我们来做一个 ...

  8. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)

    在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ...

  9. 从一个简单案例上手Spring MVC,同时分析Spring MVC面试问题

    很多公司都会用Spring MVC,而且初级程序员在面试时,一定会被问到这方面的问题,所以这里我们来通过一个简单的案例来分析Spring MVC,事实上,我们在培训中就用这个举例,很多零基础的程序员能 ...

随机推荐

  1. MyBatis源码骨架分析

    源码包分析 MyBatis 源码下载地址:https://github.com/MyBatis/MyBatis-3 MyBatis源码导入过程: 下载MyBatis的源码 检查maven的版本,必须是 ...

  2. 5.AVStream和AVCodecParameters

    AVStream和AVCodecParameters 说明: AVStream 结构表示当前媒体流的上下文,着重于所有媒体流共有的属性(并且是在程序运行时才能确定其值)和关联其他结构的字段. 其中co ...

  3. 尤雨溪:TypeScript不会取代JavaScript

    来源 |evrone.com译者 | 核子可乐策划 | 蔡芳芳 近日,Evrone 与 Vue.js 的作者尤雨溪进行了一次访谈,了解他对于无后端与全栈方法.以及 Vue.js 适用场景的看法,还有他 ...

  4. python中使用mock模块返回数据

    mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. mock在python3中已经被集成到了unittest单元测试框架中,所以,可以直接使用. m ...

  5. python 3 字典

    字典,是可变的无序集合,同时是一种以键值对为基本元素的可以存储各种数据类型的集合,用大括号({})表示字典的开始和结束,元素之间用(,)分隔. 键值对,由键(Key)和值(Value)组成,中间用冒号 ...

  6. Git | Git入门,成为项目管理大师(一)

    大家好,周一我们迎来了一个新的专题--git. 写这个专题的初衷有两点,第一点是觉得好像很少有公众号提到git相关的技术,可能是觉得太基础了看不上.但实际上git非常重要,在我们实际的开发工作当中使用 ...

  7. 三、spring boot开发web应用-使用传统的JDBC

    上一节<spring boot第一个web服务>中我们只是简单的展示了spring mvc的功能,并没有涉及到具体的CRUD的操作,也没有涉及到数据持久化的方面.本节中我们将基于原始的JD ...

  8. [Leetcode]585. 2016年的投资(MySQL)

    题目 写一个查询语句,将 2016 年 (TIV_2016) 所有成功投资的金额加起来,保留 2 位小数. 对于一个投保人,他在 2016 年成功投资的条件是: 他在 2015 年的投保额 (TIV_ ...

  9. [LeetCode]面试题14- I. 剪绳子(DP/贪心)

    题目 给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m.n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]...k[m] .请问 k[0]k[1]...* ...

  10. P4742 【[Wind Festival]Running In The Sky】

    相信来做这道题的人肯定都学过\(Tarjan\)缩点吧,如果没有建议先去做P3387 [模板]缩点,如果你忘了,建议也去看看 满足上面要求后,你会惊奇发现,这两道题基本一样,唯一的差别就是这道题需要记 ...