使用Myeclipse创建 spring+spring mvc 项目
2023-09-11 14:20:19 时间
回顾下 Java spring+spring mvc 项目的简单创建
目录
一、创建Java web项目
1.打开Myeclipse开发软件,鼠标点击 File=> New=>Web Project
2.输入项目名和相关配置,创建JavaEE Web项目
最后点击Finish,生成java web项目。
二、配置 Spring 和 Spring MVC
1.导入Spring的jar
下载地址:
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring
我下载的版本是:
下载完后,进行解压,复制spring-framework-5.2.6.RELEASE\libs文件下的扩展名为.jar的所有文件,粘贴到项目的lib文件夹下。
2.配置Spring和Spring MVC
2.1配置Spring
(1)web.xml中添加Spring的基本配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>shiro1</display-name>
<!-- 1.Spring 配置 -->
<!-- contextConfigLocation参数用来指定Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring核心监听器,默认会以 applicationContext.xml作为配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ContextLoaderListener的作用是什么? ContextLoaderListener的作用就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。
简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。
-->
</web-app>
(2)新建applicationContext.xml文件
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
2.2配置Spring MVC
(1)web.xml中添加Spring MVC的基本配置
<!-- 2.Spring MVC servlet 配置 -->
<!-- 配置一个servlet -->
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>springMvc</servlet-name>
<!-- servlet的类全名:包名+简单类名 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数,指定 Spring MVC servlet配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<!-- servlet的映射配置-->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称一致 -->
<servlet-name>springMvc</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
web.xml是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。
-->
注意:
(2)新建spring-servlet.xml 文件,配置Spring MVC
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Spring MVC的基本配置 -->
<!-- 扫描包 -->
<context:component-scan base-package="com.example.shiro2"></context:component-scan>
<!-- 将静态资源交由默认的servlet处理 -->
<mvc:default-servlet-handler />
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(3)在src下新建 包:com.example.shiro2(上面扫描包 的配置一致)
配置完成!!
三、进行测试
1.在com.example.shiro2包下,新建controller,编写一个方法:
2.然后新建一个user.jsp文件,简单编写jsp页面内容:
3.启动项目,在浏览器网址中输入http://localhost/shiro2/index,进行测试:
访问成功,说明配置成功。
相关文章
- 记spring boot线上项目内存优化
- 解决IDEA使用Spring Initializr创建项目时,无法连接到https://start.spring.io的问题
- Spring boot项目使用Base64加密,部署到Windows系统tomcat中遇到中文乱码问题
- 深入实践Spring Boot3.1.2 关系实体建模
- CSDN日报190815:spring boot 项目实战,热门技术干货分享!
- 推荐 7 个牛哄哄 Spring Cloud 实战项目
- Spring Cloud项目实战Spring Cloud视频教程 含源码
- Spring Boot 2.3.3 正式发布!67 处 bug 修复
- spring batch(二):核心部分(1):配置Spring batch
- Spring Boot 项目维护全局json数据
- maven的java web项目启动找不到Spring ContextLoaderListener的解决办法
- Spring Cloud Config采用数据库存储配置内容
- 江帅帅:Spring Boot 底层级探索系列 01- 搭建项目
- Spring集成Redis集群(含spring集成redis代码)
- 项目中Spring注入报错小结
- 【转】spring官方为什么放弃spring social项目及替代方案
- spring整合web 项目
- Spring Boot 2.2.x Junit4 升级为Junit5 后的变化、对比 找不到 org.junit.jupiter.api.Test
- Spring Boot获取前端页面参数的几种方式总结
- 经过一个多月的等待我有幸成为Spring相关项目的Contributor
- Spring 官方发起Spring Authorization Server 项目
- [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- Spring MVC(概念+项目创建+@RequestMappring)
- 全栈开发实战|人事管理系统的设计与实现(Spring Boot + Vue 3 + MyBatis)
- 【spring mvc】Spring MVC 的参数解析器ArgumentResolver阐述
- spring mvc jsonp调用示例
- InteliJ IDEA 安装spring插件 并 创建spring boot项目