一、创建项目

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. C004:要求用户输入一个美元数量,然后显示出增加5%税率后的相应金额

    程序: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { float amount; do{ printf(&q ...

  2. Hint usenl usage /*+ leading(emp,dept) usenl(emp) */

    SQL> select /*+ leading(emp,dept) usenl(emp) */ emp.*,dept.* from tb_emp03 emp,tb_dept03 dept whe ...

  3. leetcode刷题-91解码方法

    题目 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1'B' -> 2...'Z' -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1: ...

  4. Spring框架学习笔记(1)

    Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...

  5. HTML常用实体字符参考手册

    最常用的字符实体 显示结果 描述 实体名称 实体编号   空格     < 小于号 < < > 大于号 > > & 和号 & & " ...

  6. python基础:日志模块logging,nnlog

    python里面用来打印日志的模块,就是logging模块,logging模块可以在控制台打印日志,也可以写入文件中.也可以两个操作都执行 1.控制台输入 import logging#导入模块 lo ...

  7. Redis之命令详解

    Redis命令手册:http://doc.redisfans.com/

  8. JAVA知识点 I/O流框架简要笔记

    I/O 框架 流的概念 内存与存储设备之间传输数据的通道 流的分类 按方向[重点] 输入流:将<存储设备>中的内容读到<内存>中 输出流:将<内存>中的内容写到&l ...

  9. 线上问题排查-HBase写数据出现NotServingRegionException(Region ... is not online)异常

    今天线上遇到一个问题:有一台服务器的cpu持续冲高,排查发现是我们的一个java应用进程造成的,该进程在向hbase中写入数据时,日志不断地打印下面的异常: org.apache.hadoop.hba ...

  10. Flutter学习四之实现一个支持刷新加载的列表

    上一篇文章用Scaffold widget搭建了一个带底部导航栏的的项目架构,这篇文章就来介绍一下在flutter中怎么实现一个带下拉刷新和上拉加载更多的一个列表,这里用到了pull_to_refre ...