【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,事实上,我们在培训中就用这个举例,很多零基础的程序员能 ...
随机推荐
- Agumaster页面样式就绪
- C#开发PACS医学影像处理系统(一):开发背景和说明
本系列文章将从以下模块和大家分享和讨论使用C#开发医学软件PACS和RIS系统, 国内相关资料比较少,也借此机会丰富一下医学软件开发生态,讨论技术难点,希望大家互相帮助共同进步. 章节介绍及截图预览: ...
- Java单例模式的实现与破坏
单例模式是一种设计模式,是在整个运行过程中只需要产生一个实例.那么怎样去创建呢,以下提供了几种方案. 一.创建单例对象 懒汉式 public class TestSingleton { // 构造方法 ...
- python 小脚本/自动重复访问网站(快速提高网页访问量)
来到csdn也快两个月了,前前后后写了20篇博客,但才1800+的访问量,其中恐怕还有300多是我自己点的 有点桑心(┬_┬) 于是打算另辟蹊径,自己刷访问量代码如下,需要自取 import urll ...
- Node.js连接MongoDB数据库
首先要启动MongoDB服务器 先找到你的mongoDb安装目录,我的如下:就在bin文件夹下创建一个data文件夹,data内包含两个空文件夹,如下: 接着回到bin文件夹处,按住shift键,右击 ...
- linux如何把普通用户添加到sudo组
sudo原理:运行命令时,系统检查/etc/sudoers 配置文件,看这个用户是否有执行sudo的权限,如果有权限,系统要求输入用户自己的密码,如果密码输入正确,系统会以root身份运行 passw ...
- JVM运行时数据区--堆
一个进程对应一个jvm实例,一个运行时数据区,又包含多个线程,这些线程共享了方法区和堆,每个线程包含了程序计数器.本地方法栈和虚拟机栈. 核心概述 1.一个jvm实例只存在一个堆内存,堆也是java内 ...
- Java虚拟机栈--栈帧
栈帧的内部结构 每个栈帧中存储着 1.局部变量表(Local Variables) 2.操作数栈(Operand Stack)(或表达式栈) 3.动态链接(Dynamic Linking)(或执行&q ...
- (专题四)06 matlab绘图选项卡
绘图选项卡 例子1--选择已有变量,绘制图形 都是按照选中的先后顺序依次确定坐标, 如果要修改绘制图形 法一,利用绘图工具和停靠图形按钮 法二,命令行窗口中输入命令 >>plottools ...
- Shell学习(四)Shell运算符
一.基础运算 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用. expr 是一款表达式计算工具,使用它能完成表达式的求值操作. #计算两个数 ...