首页 > 其他分享 >idea新建springmvc项目

idea新建springmvc项目

时间:2023-02-15 18:23:29浏览次数:33  
标签:www http springmvc spring idea springframework 新建 org schema

今天看项目代码,居然发现只有一个应用是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

相关文章

  • idea创建web项目实操
    打开idea,选择createnewproject  直接进入项目页面的小伙伴,点击file--new--project  选择创建空项目作为工作空间,next  选择项目地址,并命名项目  fin......
  • Idea如何支持写Python代码
    在IDEA中,点击【File】-【Settings...】-【Plugins】,在里面搜索python,并安装插件。(安装后需要重启有效)在IDEA中,配置pythonSDK的路径  (清除红色下划线)【Modules】......
  • 解决vue和idea端口号不同-即跨域问题
    当vue端口号为:8081,idea端口号为8080  网页响应Request解决方法在idea的在Controller类上加上如下注解:@CrossOrigin(origins={"*","null"})   ......
  • IDEA 2022.02新建web工程
    1、创建项目 2、新建代码目录创建后的目录如下:3、添加web配置此时代码目录中会多出如下目录 4、配置tomcat点击运行配置,新增tomcatserver并配置tomcat地址......
  • 【工具使用】IDEA Spring源码报Unable to find method ‘org.gradle.api.artifacts.re
    1 前言IDEA拉Spring源码,源码是Gradle管理依赖的,但是报错Unabletofindmethod'org.gradle.api.artifacts.result.ComponentSelectionReas我的IDEA有2019、2021的2 ......
  • 新建webapp目录与配置相关 创建web项目 maven 230214
    笔记实操新建webapp目录新建WEB-INF目录新建web.xml文件编写web.xml的内容<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"......
  • IDEA 的快捷键大全
    IDEA的快捷键大全目录IDEA的快捷键大全一、IntelliJIDEA快捷键大全Win版一、Ctrl快捷键二、Alt快捷键三、Shift快捷键四、Ctrl+Alt快捷键五、Ctrl+Shift快......
  • SpringMVC源码(六):Handler处理器
    在MVC请求流程中,Handler处理器依赖于HandlerMapping组件。因此在处理客户端请求时,会优先获取HandlerMapping处理器。HandlerMapping组件主要作用是根据客户端的访......
  • SpringMVC的视图
    目录ThymeleafView转发视图重定向视图视图控制器view-controllerSpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户SpringMVC视图的种类很多,......
  • IDEA git 本地分支 恢复
    方法1:找到右下角的日志,加入删除分支后没有关闭Idea在日志中会有一个DeleteBranches,点击后面的Restore选项可以恢复已删除的分支。 方法2:找到Remote远端,直接重新......