首页 文章资讯内容详情

SpringMVC简单实例(看起来有用)

2026-06-01 2 花语

本文内容纲要:

-SpringMVC简单实例(看起来有用)

SpringMVC简单实例(看起来有用)

参考:

SpringMVC基础教程简单入门实例-CSDN博客

http://blog.csdn.net/swingpyzf/article/details/8904205

SpringMVC入门教程二:

一个简单的入门实例教程

该实例的源码和实例中的jar

源码:http://download.csdn.net/detail/swingpyzf/5348563

所需要的jar:http://download.csdn.net/detail/swingpyzf/5348531

另外一篇关于SpringMVC文件上传,多文件上传:http://blog.csdn.net/swingpyzf/article/details/20230865

简单注解配置的实例:

一、创建项目:

1、建立新的动态web项目:

2、为项目命名为:SpringMVC_01

3、添加tomcat运行时环境\依赖库如果是MyEclipse的话创建web项目时就不需要此步骤

右键项目,点击BuildPath->AddLibrares:

添加完后会多出tomcat的Servlet包

4、最后添加Spring及SpringMVC所需要的jar,我添加以下jar到项目中

二、配置文件:

1、首先在web.xml中配置一个DispatcherServlet,并通过指定需要拦截的url。下面xml中配置一个拦截.html为后缀的url.

[html]viewplaincopy

MVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/classes/mvc*.* 1 MVC *.html

先配置一个servlet然后加载SpringMVC的xml文件到Spring的上下文中。然后配置servlet-mapping,servlet-name为刚刚的servlet中的配置的name,然后指定要拦截的url为*.html

2、配置Spring的上下文监听器,并且指定Spring的xml配置文件的路径。

[html]viewplaincopy

org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:root-context.xml

这里指定的路径classpath为项目编译后的classes文件中。

最终web.xml文件内容:

[html]viewplaincopy

<web-appversion="3.0"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:root-context.xml MVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/classes/mvc*.* 1 MVC *.html index.html

3、创建SpringMVC所需要的xml文件和applicationContext的xml文件,这里由于第一步中配置的servlet中init-param所需要加载的格式为:mvc*.*就是去寻找为mvc开头的文件所以创建SpringMVC的xml文件时必须要有mvc开头,我命名为:mvc-context.xml,并且按照context-param中的配置,将applicationContext文件命名为:root-context.xml;

4、配置mvc-context.xml:

首先通过import标签导入root-context.xml,然后通过component-scan标签扫描指定包名,让该包下的所有java类的spring注解生效

然后配置SpringMVC的视图渲染解析器,让其前缀为/page/后缀为.jsp这样能够SpringMVC所需要渲染的路径能够在/page/返回值.jsp中寻找。

[html]viewplaincopy

<beans:importresource="root-context.xml"/> <context:component-scanbase-package="org.swinglife.controller"></context:component-scan> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/page/"p:suffix=".jsp"> </beans:bean>

最后mvc-context.xml和root-context.xml为:

mav-context.xml:

[html]viewplaincopy

<beans:beansxmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"> <beans:importresource="root-context.xml"/> <context:component-scanbase-package="org.swinglife.controller"></context:component-scan> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/page/"p:suffix=".jsp"> </beans:bean> </beans:beans>

root-context.xml:

[html]viewplaincopy

<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-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/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

三、编写Controller

1、创建org.swinglife.controller的package,用来存放Controller类,接着新建HomeController.java,用来编写首页的Controller

2、使用注解@Controller将HomeController类定义为一个Controller,并且在方法中通过@RequestMapping(“value”)来指定所需要访问的路径或者方法名。SpringMVC可以通过一个@Controller注解将一个POJO转化为处理请求的控制器,通过@RequestMapping为控制器指定哪些需要的请求。

[java]viewplaincopy

@Controller publicclassHomeController{ /*** *首页返回至/page/home.jsp页面 *@return */ @RequestMapping("index") publicModelAndViewindex(){ //创建模型跟视图,用于渲染页面。并且指定要返回的页面为home页面 ModelAndViewmav=newModelAndView("home"); returnmav; } }

方法中定义了ModelAndView对象,通过该对象指定所需要渲染的视图为home最后返回ModelAndView将页面渲染到home.jsp中。

3、最后在WebContent目录中创建/page/home.jsp使SpringMVC能够寻找并渲染该页面视图。

[html]viewplaincopy

<%@pagelanguage="java"contentType="text/html;charset=GB18030" pageEncoding="GB18030"%> home

springmvc实例