在myeclipse搭建简易的非注解springmvc
本人初次学习springmvc,希望把学习的内容放在csdn,以防以后忘记。
环境:Myeclipse、Tomcat8.5(Myeclipse自带)、springmvc3.2.0
1.首先打开Myeclipse,通过左上角的File/New/web project建立一个web project。


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页面的内容了

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

被折叠的 条评论
为什么被折叠?



