【idea&spring mvc】搭建简易的spring mvc项目(基于maven)!
一、创建项目
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)!的更多相关文章
- 一步一步深入spring(1)--搭建和测试spring的开发环境
1.引用jar包 到spring的网站上下载spring的jar包(本文是2.5.6),解压缩后找到 使用spring必须引用的jar包 spring.jar commons-logging.jar ...
- ActiveMQ JMS 项目 基于 Maven 搭建 部署
JAVA版本: IntellJ IDEA 版本: IntelliJ IDEA 2017.2Build #IU-172.3317.76, built on July 15, 2017Licensed t ...
- ASP.NET Core ASP.NET Core+MVC搭建及部署
ASP.NET Core+MVC搭建及部署 一.创建项目: 1.选择ASP.NET Core Web Application(.NET Core) 注意框架 2.选择Web Application: ...
- Spring MVC 项目搭建 -3- 快速 添加 spring security
Spring MVC 项目搭建 -3- 快速 添加 spring security 1.添加 spring-sample-security.xml <!-- 简单的安全检验实现 --> & ...
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...
- Spring MVC 搭建web项目示例
环境为Eclipse 1:新建Dynamic web project : springMvcDemo 2:下载spring的jar包,把jar包复制到WEB-INF/lib目录下 3.添加配置文件w ...
- 25、ASP.NET MVC入门到精通——Spring.net-业务层仓储
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架. 1.假设我们来做一个 ...
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)
在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ...
- 从一个简单案例上手Spring MVC,同时分析Spring MVC面试问题
很多公司都会用Spring MVC,而且初级程序员在面试时,一定会被问到这方面的问题,所以这里我们来通过一个简单的案例来分析Spring MVC,事实上,我们在培训中就用这个举例,很多零基础的程序员能 ...
随机推荐
- 仅显示sessionid,servername,serverport的一个springboot小程序
下载地址:https://files.cnblogs.com/files/xiandedanteng/sessionid20191227-1.zip --END-- 2019-12-2710:07
- 原生 Java 客户端进行消息通信
原生 Java 客户端进行消息通信 Direct 交换器 DirectProducer:direct类型交换器的生产者 NormalConsumer:普通的消费者 MulitBindConsumer: ...
- Docker Compose 搭建 Redis Cluster 集群环境
在前文<Docker 搭建 Redis Cluster 集群环境>中我已经教过大家如何搭建了,本文使用 Docker Compose 再带大家搭建一遍,其目的主要是为了让大家感受 Dock ...
- Python测试框架pytest命令行参数用法
在Shell执行pytest -h可以看到pytest的命令行参数有这10大类,共132个 序号 类别 中文名 包含命令行参数数量 1 positional arguments 形参 1 2 gene ...
- 【Azure DevOps系列】使ASP.NET Core应用程序托管到Azure Web App Service
使用Azure DevOps Project设置ASP.NET项目 我们需要先在Azure面板中创建一个Azure WebApp服务,此处步骤我将省略,然后点击部署中心如下图所示: 此处我选择的是Az ...
- Odoo10中calendar视图点击事件
有个需求,需要根据该条记录的状态字段来控制点击calendar时是否需要打开form视图,解决方案如下:重写了web_calendar的get_fc_init_options()方法中的eventCl ...
- Linux:网络基础配置
一.修改主机名 hostname 查看主机名 1.hostname zy 修改主机名为zy,临时生效,重新登录系统生效. 2.想要永久修改,,需修改配置文件: vi /etc/sysconf ...
- JAVA JDK 环境变量配置 入门详解 - 精简归纳
JAVA JDK 环境变量配置 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 9 / 13 转载请注明出处!️ 目录 JAVA JDK 环境变量配置 入门详解 - 精简归纳 一.为什么j ...
- wsl 修改默认安装路径
如果已经装了,先删除 mklink /j C:\Users\XXXX\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rh ...
- SpringCloud OpenFeign Post请求的坑
在微服务开发中SpringCloud全家桶集成了OpenFeign用于服务调用,SpringCloud的OpenFeign使用SpringMVCContract来解析OpenFeign的接口定义. 但 ...