一、创建项目

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. PostgressQL

    环境 ubuntu18.04 docker 中 pull postgres:10 images 连接 进入容器内部 docker exec -it [mypostgres] /bin/bash 查看p ...

  2. ui自动化---CssSelector

    xpath切换到css

  3. [Java并发]实现两个线程交替打印奇偶数(volatile+yield实现)

    解题思路 实现一个类OddEven 有一个打印奇数的方法,有一个打印偶数的方法. 类中有一个volatile变量 ,用来控制当前状态是该哪个方法打印. 方法中打印每个数前首先判断volatile变量的 ...

  4. [LeetCode]1249. 移除无效的括号(字符串,栈)

    题目 给你一个由 '('.')' 和小写字母组成的字符串 s. 你需要从字符串中删除最少数目的 '(' 或者 ')' (可以删除任意位置的括号),使得剩下的「括号字符串」有效. 请返回任意一个合法字符 ...

  5. 学习使用PSTools工具中的psping

    目录 初识PsTools psping 1.使用ICMP ping 2.使用TCP ping 3.延迟测试 4.带宽测试 5.同功能的tcping工具 总结 初识PsTools     在工作中我们都 ...

  6. RXJAVA之Subject

    RxJava中常见的Subject有4种,分别是 AsyncSubject. BehaviorSubject.PublishSubject. ReplaySubject. AsyncSubject 使 ...

  7. Dubbo工作流程

    一.dubbo整体架构 其中Service 和 Config 层为 API,对应服务提供方来说是使用ServiceConfig来代表一个要发布的服务配置对象,对应服务消费方来说ReferenceCon ...

  8. JVM学习(三)JVM垃圾回收

    一.引用的分类 在了解JVM垃圾回收机制之前,了解一下对象的引用类型是非常必要的. 强引用:GC时不会被回收 软引用:描述有用但不是必须的对象,在发生内存溢出异常之前被回收 弱引用:描述有用但不是必须 ...

  9. RocketMQ系列:rocketmq运维控制台使用详解(全网独家)

    搭建好了RocketMQ的console之后,直接在浏览器打开http://{你的ip}:8080 默认会进入到驾驶舱(dashboard). 总览 整体横向菜单分为八个部分: 运维:主要是设置nam ...

  10. uni-app开发注意事项

    关于vue 1.注意 如果使用老版的非自定义组件模式,即manifest中"usingComponents":false,部分模版语法不支持,但此模式已于2019年11月起下线. ...