zl程序教程

您现在的位置是:首页 >  后端

当前栏目

使用Myeclipse创建 spring+spring mvc 项目

Spring项目MVC 创建 myeclipse 使用
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,进行测试:
在这里插入图片描述

访问成功,说明配置成功。