3分钟在myeclipse搭建简易的非注解springmvc

本文介绍在Myeclipse搭建简易非注解SpringMVC的方法。环境涉及Myeclipse、Tomcat8.5和springmvc3.2.0。详细步骤包括创建web项目、导入springmvc包、配置web.xml和springmvc.xml、创建controller、部署并运行项目,最后可在浏览器查看部署内容。

在myeclipse搭建简易的非注解springmvc

本人初次学习springmvc,希望把学习的内容放在csdn,以防以后忘记。

环境:Myeclipse、Tomcat8.5(Myeclipse自带)、springmvc3.2.0

1.首先打开Myeclipse,通过左上角的File/New/web project建立一个web project。
在这里插入图片描述

给项目取名为mySpringMvc1
2.导入springmvc的包(地址http://maven.springframework.org/release/org/springframework/spring/)
右击当前项目名,找到Build Path/Configure Build Path并打开
点击进入Libraries,然后点击Add External JARS,找到spring的相关包导入,然后Apply;
再把这些包复制到WebRoot/WEB-INF/lib里面。
在这里插入图片描述在这里插入图片描述
3.在当前项目的WebRoot/WEB-INF下建立一个web.xml,并把如下内容粘贴进去。

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<!-- 加载springmvc配置 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>

			<!-- 配置文件的地址 -->
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 处理器映射器 -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>	

4.在当前项目下建立一个资源文件夹(注意是资源文件夹,也就是sourceFolder而不是普通文件夹Folder)名为config,然后在此文件夹创建一个xml文件名为springmvc.xml,并把如下内容粘贴进去。

<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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
		
		
    <!-- 配置handler -->
        <bean class="com.controller.helloSpringMvc" name="/index.action" />
    
        	
    <!-- 配置处理器映射器 -->
      	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
      	 
    <!-- 配置处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
       
    <!-- 配置视图解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		</bean>
</beans>

在这里插入图片描述
5.创建controller,在当前项目的src目录中创建一个包名为com.controller,并在此包创建一个class名为helloSpringMvc并将如下内容复制过去。
在这里插入图片描述

package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class helloSpringMvc implements Controller 
{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		// TODO Auto-generated method stub
		ModelAndView mav=new ModelAndView();
		mav.setViewName("index.jsp");
		return mav;
	}

}

6.部署项目并运行。点击目录栏的Manage Deployments按钮。然后选择Module为当前项目(即mySpringMvc),然后点击add添加服务器,选择Myeclipse自带的服务器Myeclipse Tomcat。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
7.点击运行按钮运行服务器
在这里插入图片描述
选择刚才的Myeclipse tomcat。
在这里插入图片描述
待服务器运行成功后,即在控制台看到如下提示。
在这里插入图片描述
打开浏览器,输入http://localhost:8080/mySpringMvc1/index.action
回车,就可以看到部署的项目的index.jsp页面的内容了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值