今天看项目代码,居然发现只有一个应用是springboot,剩下的居然全是springmvc,好多年没有碰过springmvc了,怎么启动都快忘了,今天从头操作了个demo,再idea中新建一个spirngmvc项目,能够暴露restful请求
第一步:先下载tomcat,和同事要了一个压缩包,免安装的
第二步:新建module,选择maven类型,勾选根据模板创建,webapp这个
下一步直至结束,得到如下结构的项目
再main的目录下创建java和resources文件夹
第三部,处理项目配置文件
pom.xml中添加依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
按理说,speing-webmvc包包含了spring-web的包,但是后来发现写controller用注解RestController时,还是报红,说没有这个注解,需要添加依赖包spring-web,没办法又在pom.xml添加了spring-web的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
编辑web.xml和新建springmvc.xml
web.xml文件开始如下:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
添加servlet和servlet-mapping
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 申明哪些请求会被我当前的Servlet处理-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
sources文件夹右键新建xml,里面会有springmvc配置文件模板
新建的文件内容:
<?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.xsd">
</beans>
添加context:component-scan和<mvc:annotation-driven> component-sacan里配置好扫描的根目录
注意:下面beans里的命名空间要写全,否则会报错:”通配符的匹配很全面, 但无法找到元素 ‘context:component-scan‘ 的声明。”
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.dragon.springmvcdemo"> </context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
再java文件夹下创建相应目录:com.dragon.springmvcdemo,在里面写一个restful接口的controller:
package com.dragon.springmvcdemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/controller")
public class HelloController {
@RequestMapping("/sayhello")
public String sayHello(){
return "++++++++++hello++++++++++++++";
}
}
idea的run菜单找到endit Condigurations,编辑添加tomcat服务
配置好名字和deploymen
启动tomcat
启动日志如果有乱码,则修改tomcat目录的conf下的logging.properties文件
这几处改为GBK
启动成功后浏览器自动打开页面
注意这个根目录,如果访问写好的接口,会报错:
http://localhost:8080/controller/sayhello
这是因为再tomcat服务器的deploymen页面有个根目录设置,默认的是:http://localhost:8080/springmvcdemo_war_exploded
所以要么把这个根目录改成空,则可以访问http://localhost:8080/controller/sayhello,或者再设置好的根目录下添加RequestMapping:http://localhost:8080/springmvcdemo_war_exploded/controller/sayhello即可,最后成功访问
标签:www,http,springmvc,spring,idea,springframework,新建,org,schema From: https://www.cnblogs.com/gaokunlong/p/17124220.html