在idea中不使用maven,创建springmvc 项目(使用注解)-3(不是maven项目,太繁琐,不推荐)

本文主要是介绍在idea中不使用maven,创建springmvc 项目(使用注解)-3(不是maven项目,太繁琐,不推荐),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一.介绍说明

1.因为不是使用maven创建项目,所以在找包时候特别繁琐。工程的结构如下:

2.在最后面,可以看到:

        有些图片加载不出来,发现凡是放到WEB-INF下的静态资源,images图片,均无法加载出来。 

      和WEB-INF的平级static/images/aa.jpg下的图片,能够读取访问的到。

所以说:idea中的springmvc 无法加载WEB-INF下的静态资源

二.新建项目 

2.1 新建项目

勾选Spring MVC 、Web Application(勾选了Spring MVC后IDEA默认会勾选Web Application),Next

2.2 在lib包中添加所有需要jar包

 

解压到自己创建的lib文件夹里,注意是WEB-INF下自己创建的,不是外部的lib

解压后右键lib文件夹,然后如图操作

2.3 需要在web目录下新建classes和lib,jsp等文件夹

这里的lib,我直接将src上面的lib包,复制到了web-inf下面的lib中

step2: login.jsp页面代码如下:

<%--Created by IntelliJ IDEA.User: jurflDate: 2020/9/12Time: 11:36To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>helloworld</title>
</head>
<body>
<h2>${helloMsg}</h2>
<div><img src="./static/images/aa.jpg"></img><img src="${pageContext.request.contextPath}/static/images/aa.jpg"></img><img src="./images/bb.jpg"></img><div>有问题的:</div><img src="./static/images/cc.jpg"></img><img src="../static/images/cc.jpg"></img><img src="../../static/images/aa.jpg"></img><img src="../images/bb.jpg"></img></div>
</body>
</html>

 2.5 需要在web目录下存储图片的目录

2.6 在web-info下配置web.xml的内容

<?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"><!--<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/dispatcher-servlet.xml</param-value></context-param>--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--DispatcherServlet--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--encodingFilter--><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

2.7 新建xxx-servlet.xml文件

注意这里xxx-servlet.xml的名字命名要和web.xml中配置的servlet的名字一样,如   

<!--DispatcherServlet-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>

。。。。。

所以的配置文件名为dispatcherServlet-servlet.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"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 1.开启SpringMVC注解驱动 --><mvc:annotation-driven /><!-- 2.静态资源默认servlet配置--><mvc:default-servlet-handler/><!-- 3.配置jsp 显示ViewResolver视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 4.扫描web相关的bean --><context:component-scan base-package="com.test" /><!--    添加MVC 加载静态文件 css、图片等--><!--<mvc:resources location="/WEB-INF/images/" mapping="/images/**"/><mvc:resources location="/WEB-INF/js/"  mapping="/js/**"/><mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>--><mvc:annotation-driven /></beans>

2.8 新建一个applicationContext.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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</beans>

2.9  配置controller层

package com.test.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;/*** @ClassName: UserController* @Description: TODO* @Author: liujianfu* @Date: 2020/09/12 14:49:27 * @Version: V1.0**/
@Controller
public class UserController {@RequestMapping("/index")public String index(Model model) {model.addAttribute("helloMsg", "HelloWorld,你好世界");return "login";}
}

 2.10 配置index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>spring-mvc:登录页面</title>
</head>
<body>
<h2>欢迎登录</h2>
<div><a href="${pageContext.request.contextPath}/index">去主页</a>
</div>
</body>
</html>

 三.进行tomcat相关配置

3.1 Output path配置(此步可跳过): 

3.2 相关jar文件夹配置:

 

 

3.3 Library相关配置:  

四.设置artifacts的war包需要lib

五.tomcat部署发布

step1

step2: 

step3:

 六.启动服务,访问页面

step1:启动成功

 

#访问页面

#点击index页面的“去主页”,到login页面 

这篇关于在idea中不使用maven,创建springmvc 项目(使用注解)-3(不是maven项目,太繁琐,不推荐)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013197

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释