首页 > 其他分享 >如何用idea新建一个Spring web项目

如何用idea新建一个Spring web项目

时间:2022-11-18 11:59:38浏览次数:69  
标签:xml web springmvc Spring idea springframework ModelAndView org

如何用idea新建一个Spring web项目

首先要下好maven,tomcat,idea,jdk等开发工具
一、新建一个工程

1、打开idea,选择File->New->project
image
2、选择一个空项目,点击next,给项目命名,点击Finish即可
image
3、出现以下界面,点击左上角的+号,选择new module
image
4、选择Java EE选择,点击next,点击Finish->OK即可
image
5、项目建好后的目录结构为
image

二、配置tomcat

1、进入tomcat编辑页
image
2、进行tomcat配置,如果Application server为空,点击configure进行配置
image
image

三、引入依赖

1、向项目中引入两个依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.15</version>
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.15</version>
  </dependency>
四、配置前端控制器

1、单击web.xml文件节点,编辑该文件
image
web.xml文件的内容时告诉Web容器,将使用Spring MVC的DispatherServlet,并通过url-patter元素的值为"/",将所有的URL映射到该文件上

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     version="4.0">

<!-- 定义springMvc的前端控制器 -->
<servlet>
    <!-- 这段配置就是在启动服务加载springmvc的disservlet ,初始化了参数,就我画那部分-->
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springmvc-config.xml</param-value>
    </init-param>
</servlet>
<!-- 让springMVC的前端控制器拦截所请求 -->
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

2、在WEB-INF页面新建一个springmvc-config.xml文件,配置Spring MVC的controller
1、以基于注解的控制器为例,配置springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
   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.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<!-- spring 可以自动扫描base-pack下面的包或者子包下面的java文件,如果扫描到spring的相关注解类,则把这些类注册为spring的bean -->
<context:component-scan base-package="com.yc.springmvc.Controller" />
<!-- 配置annotation类型的处理器映射 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- 配置annotation类型的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>

image

五、Controller类的实现

1、新建一个HelloController
image

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
//@RequestMapping用来映射请求的URL和请求的方法等,本例用来映射"/hello"
@RequestMapping("/hello")
public ModelAndView helloWorld(){
    System.out.println("hello 方法被调用");
    //创建准备返回的ModelAndView对象,该对象通常包含了返回视图名,模型的名称以及模型对象
    ModelAndView mv=new ModelAndView();
    //添加模型数据,可以说任意的POJO对象
    mv.addObject("message","hello World");
    //设计视图名,视图解析器会根据该名称解析到具体的视图页面
    mv.setViewName("/WEB-INF/content/welcome.jsp");
    //返回ModelAndView对象
    return mv;
}
}
六、在WEB-INF下新建一个content目录,新建welcome.jsp

image

七、启动服务

image

7、在浏览器中输入访问地址:http://localhost:80/hello

image

注意:如果配置端口为80,80端口有可能不会显示

出来基于注解的配置文件spring-config.xml,还有基于Controller接口的控制器
参考文章:springmvc配置文件web.xml和spring-config.xml

标签:xml,web,springmvc,Spring,idea,springframework,ModelAndView,org
From: https://www.cnblogs.com/lil4257/p/16901768.html

相关文章

  • 自学 TypeScript 第三天 使用webpack打包 TS 代码
    前言:大家好啊,昨天介绍了TS编译器的配置,但在我们实际开发当中直接使用TS编译器去编译代码的情况会有,但没有很多,因为我们在开发大型项目的时候,一般我们都会用到打包工具......
  • Redisson在SpringBoot的使用配置
    本文介绍如何在SpringBoot中配置Redisson官网网址redisson-spring-boot-starter配置方案:redisson/redisson-spring-boot-starteratmaster·redisson/redisson·Git......
  • idea 设置vue@跳转
    第一步:根目录下面,创建一个JS文件,名字随便起config.js/*此文件未使用,只是为了让idea可以识别实际位置*/constpath=require('path');functionresolve(dir){returnpa......
  • 用SpringMVC 实现断点续传 (HTTP)
    ​ 一、概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载。在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了。一般断点下载时才用......
  • iis 反向代理 API以及websocket
    前言:前端的系统一般不支持跨域,需要我们部署的时候配置下反向代理。这边介绍下iis反向代理,iis反向代理是通过iis的URL重写模块来重写URL从而访问目标服务器的。假设我们......
  • springboot自动填充配置(创建更新时间)
    springboot自动填充配置很多数据库的表大多都会设置两个固定的字段:create_time和update_time而每次进行业务操作比如更新或插入数据的时候,都要给这两个字段插入数据。......
  • JavaCV音视频开发宝典:vb8和vp9编码的webm格式视频文件转成mp4文件
    本文转载自:https://blog.csdn.net/eguid_1/article/details/125251492《JavaCV音视频开发宝典》专栏目录导航《JavaCV音视频开发宝典》专栏介绍和目录​前言mp4不用......
  • web入门(二)
    现在我们进入到了爆破模块的学习目录web21web22web23python脚本php脚本web24web25web26web27web28web21进入网站,提示要登陆,妥爆破抓包注意到最下行的base64编码发现......
  • SpringBoot整合Junit,MyBatis, druid
    整合JUnit在要测试的类前加上@Respository     在里面写要测试的类名  整合MyBatis:1.创建的时候勾选上mybatisframework,sql.spring就自动创建了depa......
  • springboot 配置 OpenFeign 时报错:Servlet.service() for servlet [dispatcherServlet
    报错内容如下:2022-11-1801:55:18.998ERROR22220---[nio-8086-exec-4]o.a.c.c.C.[.[.[/].[dispatcherServlet]:Servlet.service()forservlet[dispatcherServlet......