SpringMVC基础教程简单入门实例介绍



SpringMVC基础教程简单入门实例介绍。

pring MVC 入门教程二:

一个简单的入门实例教程

该实例的源码和实例中的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项目时就不需要此步骤

右键项目,点击Build Path->Add Librares:

添加完后会多出tomcat 的 Servlet包

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

 

二、配置文件:

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

 

  1. <!– 配置Spring MVC DispatcherServlet –>
  2.     <servlet>
  3.         <servlet-name>MVC</servlet-name>
  4.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5.         <!– 初始化参数 –>
  6.         <init-param>
  7.             <!– 加载SpringMVC的xml到 spring的上下文容器中 –>
  8.             <param-name>contextConfigLocation</param-name>
  9.             <param-value>
  10.                 /WEB-INF/classes/mvc*.*
  11.             </param-value>
  12.         </init-param>
  13.         <load-on-startup>1</load-on-startup>
  14.     </servlet>
  15.     <!– 配置DispatcherServlet所需要拦截的 url –>
  16.     <servlet-mapping>
  17.         <servlet-name>MVC</servlet-name>
  18.         <url-pattern>*.html</url-pattern>
  19.     </servlet-mapping>

 

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

 

 

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

 

  1. <!– 监听spring上下文容器 –>
  2. <listener>
  3.     <listener-class>
  4.         org.springframework.web.context.ContextLoaderListener
  5.     </listener-class>
  6. </listener>
  7. <!– 加载spring的xml配置文件到 spring的上下文容器中 –>
  8. <context-param>
  9.     <param-name>contextConfigLocation</param-name>
  10.     <param-value>classpath:root-context.xml</param-value>
  11. </context-param>

 

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

 

最终web.xml文件内容:

 

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”>
  6.     <display-name></display-name>
  7.     <!– 监听spring上下文容器 –>
  8.     <listener>
  9.         <listener-class>
  10.             org.springframework.web.context.ContextLoaderListener
  11.         </listener-class>
  12.     </listener>
  13.     <!– 加载spring的xml配置文件到 spring的上下文容器中 –>
  14.     <context-param>
  15.         <param-name>contextConfigLocation</param-name>
  16.         <param-value>classpath:root-context.xml</param-value>
  17.     </context-param>
  18.     <!– 配置Spring MVC DispatcherServlet –>
  19.     <servlet>
  20.         <servlet-name>MVC</servlet-name>
  21.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22.         <!– 初始化参数 –>
  23.         <init-param>
  24.             <!– 加载SpringMVC的xml到 spring的上下文容器中 –>
  25.             <param-name>contextConfigLocation</param-name>
  26.             <param-value>
  27.                 /WEB-INF/classes/mvc*.*
  28.             </param-value>
  29.         </init-param>
  30.         <load-on-startup>1</load-on-startup>
  31.     </servlet>
  32.     <!– 配置DispatcherServlet所需要拦截的 url –>
  33.     <servlet-mapping>
  34.         <servlet-name>MVC</servlet-name>
  35.         <url-pattern>*.html</url-pattern>
  36.     </servlet-mapping>
  37.     <welcome-file-list>
  38.         <welcome-file>index.html</welcome-file>
  39.     </welcome-file-list>
  40. </web-app>

 

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中寻找。

 

  1. <!– 加载Spring的全局配置文件 –>
  2.     <beans:import resource=”root-context.xml” />
  3.     <!– SpringMVC配置 –>
  4.     <!– 通过component-scan 让Spring扫描org.swinglife.controller下的所有的类,让Spring的代码注解生效 –>
  5.     <context:component-scan base-package=”org.swinglife.controller”></context:component-scan>
  6.     <!– 配置SpringMVC的视图渲染器, 让其前缀为:/page/ 后缀为.jsp  将视图渲染到/page/<method返回值>.jsp中 –>
  7.     <beans:bean
  8.         class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
  9.         p:prefix=”/page/” p:suffix=”.jsp”>
  10.         </beans:bean>

 

 

 

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


mav-context.xml:

 

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans:beans xmlns=”http://www.springframework.org/schema/mvc”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:beans=”http://www.springframework.org/schema/beans”
  4.     xmlns:p=”http://www.springframework.org/schema/p” xmlns:aop=”http://www.springframework.org/schema/aop”
  5.     xmlns:context=”http://www.springframework.org/schema/context”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  7.                 http://www.springframework.org/schema/aop
  8.                 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>
  10.     <!– 加载Spring的全局配置文件 –>
  11.     <beans:import resource=”root-context.xml” />
  12.     <!– SpringMVC配置 –>
  13.     <!– 通过component-scan 让Spring扫描org.swinglife.controller下的所有的类,让Spring的代码注解生效 –>
  14.     <context:component-scan base-package=”org.swinglife.controller”></context:component-scan>
  15.     <!– 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.jsp  将视图渲染到/page/<method返回值>.jsp中 –>
  16.     <beans:bean
  17.         class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
  18.         p:prefix=”/page/” p:suffix=”.jsp”>
  19.         </beans:bean>
  20. </beans:beans>

root-context.xml:

 

 

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  5.                 http://www.springframework.org/schema/context
  6.                  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd”>
  8.     <!– Root Context: defines shared resources visible to all other web components –>
  9. </beans>

 

三、编写Controller

 

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

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

 

  1. @Controller
  2. public class HomeController {
  3.     /***
  4.      * 首页 返回至/page/home.jsp页面
  5.      * @return
  6.      */
  7.     @RequestMapping(“index”)
  8.     public ModelAndView index(){
  9.         //创建模型跟视图,用于渲染页面。并且指定要返回的页面为home页面
  10.         ModelAndView mav = new ModelAndView(“home”);
  11.         return mav;
  12.     }
  13. }

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

 

 

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

  1. <%@ page language=”java” contentType=”text/html; charset=GB18030″
  2.     pageEncoding=”GB18030″%>
  3. <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” ”http://www.w3.org/TR/html4/loose.dtd”>
  4. <html>
  5. <head>
  6. <meta http-equiv=”Content-Type” content=”text/html; charset=GB18030″>
  7. <title>home</title>
  8. </head>
  9. <body>
  10. <h2>spring mvc 实例</h2>
  11. </body>
  12. </html>

现在一个完整的SpringMVC的模式已经搭建完成了,可以运行项目来进行测试。

 

 

四、编写参数的提交与传递:

 

1、编写一个新的UserController类来假定用户登录,将username,password提交到Controller中进行处理,并且登陆成功后将username,password传递到成功的页面。

创建UserController.java

创建/page/succ.jsp页面 作为用户成功登陆页面

UserController中的代码:

 

  1. @Controller
  2. public class UserController {
  3.     /***
  4.      * 用户登陆
  5.      * <p>注解配置,只允许POST提交到该方法
  6.      * @param username
  7.      * @param password
  8.      * @return
  9.      */
  10.     @RequestMapping(value=”login”,method=RequestMethod.POST)
  11.     public ModelAndView login(String username,String password){
  12.         //验证传递过来的参数是否正确,否则返回到登陆页面。
  13.         if(this.checkParams(new String[]{username,password})){
  14.             //指定要返回的页面为succ.jsp
  15.             ModelAndView mav = new ModelAndView(“succ”);
  16.             //将参数返回给页面
  17.             mav.addObject(“username”,username);
  18.             mav.addObject(“password”, password);
  19.             return mav;
  20.         }
  21.         return new ModelAndView(“home”);
  22.     }
  23.     /***
  24.      * 验证参数是否为空
  25.      * @param params
  26.      * @return
  27.      */
  28.     private boolean checkParams(String[] params){
  29.         for(String param:params){
  30.             if(param==”"||param==null||param.isEmpty()){
  31.                 return false;
  32.             }
  33.         }
  34.         return true;
  35.     }

首先指定@Controller,然后指定@RequestMapping为login方法;

 

需要注意的是这次@RequestMapping中指定了页面方法模式必须为POST模式否则将无法访问。其次value参数指定访问路径。

并且在login方法中设定带参,参数为表单中的name属性。

然后通过ModelAndView的 addObject方法将参数加入到request中,这样则能够在返回的页面中显示这些参数。

在此之外还有其他将参数传递到页面中的方式为:

 

  1. /***
  2.  * 另一种参数传递的形式
  3.  * 通过request来处理请求过来的参数。
  4.  * @param username
  5.  * @param password
  6.  * @param request
  7.  * @return
  8.  */
  9. @RequestMapping(value=”login”,method=RequestMethod.POST)
  10. public ModelAndView login(String username,String password,HttpServletRequest request){
  11.     request.setAttribute(“username”, username);
  12.     request.setAttribute(“password”, password);
  13.     return new ModelAndView(“succ”);
  14. }

以上这种方式则是直接通过将参数加入到request中来使用。

 

 

2、编写succ.jsp页面跟表单页面:

succ.jsp:

 

  1. <body>
  2. <h2>登陆</h2>
  3. username:${username }
  4. <p>
  5. password:${password }
  6. </body>

 

 

form:

 

  1. <form action=”login.html” method=”post”>
  2.     username:<input type=”text” name=”username” />
  3.     <p>
  4.     password:<input type=”password” name=”password”/>
  5.     <p>
  6.     <input type=”submit” value=”submit” />
  7. </form>

3、最后运行项目来进行测试:

 

 

 

OK都完成了。以上就是一个比较简单的SpringMVC的示例搭建了。

在给出的源码中,还有另一中直接用String当做返回值来指定显示页面的方法。