在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对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.