spring-mvc!

一早上来公司上班,又是没网的节奏!什么又做不了!

想起昨天了看了些spring-mvc的文档。

就来配置下spring-mvc。

由于没有网络,那就只有spring的官方文档了。

1. 先是建立个基本工程就ok了。

2. 引入spring-mvc的开发包。根据开发文档只需要三个包。

以下是spring的文档:

Copy libraries to 'WEB-INF/lib'

First create a 'lib' directory in the'war/WEB-INF' directory. Then, from the Spring distribution, copyspring.jar (from spring-framework-2.5/dist) andspring-webmvc.jar (fromspring-framework-2.5/dist/modules) to the new 'war/WEB-INF/lib' directory. Also, copy commons-logging.jar(from spring-framework-2.5/lib/jakarta-commons) to the'war/WEB-INF/lib' directory. These jars will be deployed to the server and they are also used during the build process.

3. 修改web.xml,添加一个servlet.如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>

</web-app>


4 在WEB-INF 下新建springmvc-servlet.xml.这里的命名为你的工程加上servlet.

   然后在文档中配置你需要的controller.

 

<?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-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->

  <!-- 
        控制器,hello.htm的路径,都会经过该控制器。有点像在struts2中struts.xml中配置action的感觉。
	每一个路径对应个cotroller.在srping-mvc中没有action的概念,只有cotroller.
   -->
  <bean name="/hello.htm" class="springapp.web.HelloController"/>

</beans>

5 建立controller.并在webroot下面建立hello.jsp.

package springmvc.web;


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

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



public class HelloController implements Controller{

	protected final Log logger = LogFactory.getLog(getClass());
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		logger.info("run hello Controller");
		return new ModelAndView("hello.jsp");
	}
}


然后在浏览器中输入 http://localhost:8080/springmvc/hello.htm.即可显示hello.jsp页面。


 

 

 

 

 

posted @ 2013-08-15 10:28  梦你所梦  阅读(168)  评论(0编辑  收藏  举报