首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

springmvc配备 新手入门教程

2012-10-20 
springmvc配置 新手入门教程web.xml文件?xml version1.0 encodingUTF-8?web-app xmlnshttp://j

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>


Xml代码 
<?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> 




Java代码 
package cn.itcchina.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
 

热点排行