springmvc配置 新手入门教程

本文介绍了一个简单的SpringMVC应用配置实例,包括web.xml和spring-servlet.xml的设置,以及控制器类的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

闲来无事,将自己经常用到的Springmvc做个示例,同时也当是一个总结。平时我们大都使用的mvc框架大都是彩Struts、JSF之类的。其实spring也有自己的mvc实现。我的工作当中就使用的这个东西,感觉还不错!可惜大部分的配置都是同事已配置好的,自己对这个springmvc的配置还真不是很明白。为了让掌握它,所以就有了这个示例。

 

配置其实很简单,先来看一下web.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
         version="2.5">
    <servlet>
        <!--通过此处的命名,spring会自动去WEB-INF/下寻找 此名+ -servlet.xml 文档
        如此处,我用spring做为名字,则它会自动去找匹配的WEB-INF/spring-servlet.xml文档-->
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <!--配置一个请求后缀,凡是以html结尾的路径,都会被springmvc拦截-->
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <!--配置spring时需指定要加载的配置文件,文件内容可以为空-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:application-main.xml</param-value>
    </context-param>
    <!--这个东西不知道有什么用,项目当中有用到,这里没加也没问题。可能以后有用
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>-->
   <!-- <servlet>
         这个东西不知道有什么用,项目当中有用到,这里没加也没问题。可能以后有用
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>-->
    <!--首页-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
 

 

通过上面的注释,可以看出还需要一个叫spring-servlet.xml文件,这个文件在WEB-INF下面,再强调一下,spring会自动去WEB-INF下寻找上面已命名的servlet名字(如上面的"spring")加上“-servlet.xml”的文件.这个文件里放的最主要的是一此类似struts下的action类。内容如下:

 

 

<?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">

    <!--这是springmvc一种简单的请求方式,还有更多方式可以配置。这里的name就是用户
    可以在前台访问的路径。如访问http://localhost:8080/index.html,则会自动 
    跳转到这个控制器里-->
    <bean name="/index.html" class="cn.oyangk.web.action.IndexCtrl">
    </bean>
</beans>
 

再来看IndexCtrl这个控制器的内容:

 

package cn.oyangk.web.action;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

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

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-6-30
 * Time: 23:14:09
 */
public class IndexCtrl implements Controller{
   private final static Log logger = LogFactory.getLog(IndexCtrl.class);
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       logger.info("hello.jsp is run..");
        return new ModelAndView("/WEB-INF/jsp/index.jsp");  
    }
}

 非常简单的一个类,实现了Controller接口,(也可以使用其它方式,实现这个Action)。在这个类中需重写handleRequest()方法。这个方法直接返回一个页面。到此springmvc的配置完成了。非常简单吧,而且连log4j也自动配置好了!

所需要的jar包也非常少。最主要的加这个jar包:spring-webmvc-2.5.jar