Spring MVC的简单实例

开发环境:MyEclipse,Spring2.0

(1)新建一个WebProject,命名为myApp,导入所需要的jar包:

在工程名上点右键-->BuildPath-->ConfigureBuildPath-->Libraries-->Add External Jars,导入spring.jar,commons-logging.jar,log4j-1.2.9.jar这3个包。(好像在Spring3.0以上的版本中,就没有spring.jar这个包了,取而代之的是一些分开的小包)。

(2)修改WEB-INF目录下web.xml文件,内容如下



		<servlet>
		
			<servlet-name>dispatcherServlet</servlet-name>
			
			<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
			
			<!-- 初始化参数 -->
			<!--  不知道为什么,如果加上下面的初始化参数,
			运行时就会报404错:dispatcherservlet is not available
		<init-param> <param-name>contextConfigLocation</param-name>   
		<param-value>/WebRoot/WEB-INF/dispatcherServlet-servlet.xml</param-value> 
		</init-param> <load-on-startup>1</load-on-startup>   -->
		
		</servlet>
		
		<!-- 拦截所有以do结尾的请求 -->
		
		<servlet-mapping>
			<servlet-name>dispatcherServlet</servlet-name>
			<url-pattern>*.do</url-pattern>
		</servlet-mapping>



(3)编写实现输出的JSP页面index.jsp,放在WebRoot目录下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
	<head>
		<title>第一个SpringMVC实例</title>
	</head>
	<%
	String str = (String) request.getAttribute("helloWorld");
	%>
	<body>
		<H1><%=str%></H1>
	</body>
</html>







(4)编写控制器HelloWorldAction.java,放在src目录下的com.jp.action包里

在Spring里,所有控制器最终都可以看作是实现了Controller接口。下面代码中的handleRequest()方法返回一个ModelAndView,用来显示返回的页面和页面上的内容


package com.jp.action;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

//实现了Controller接口,需要在导入spring.jar包
public class HelloWorldAction implements Controller {
	private Logger logger = Logger.getLogger(this.getClass().getName());
	private String helloWorld;
	private String viewPage;

	public String getHelloWorld() {
		return helloWorld;
	}

	public void setHelloWorld(String helloWorld) {
		this.helloWorld = helloWorld;
	}

	public String getViewPage() {
		return viewPage;
	}

	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}

	// 实现Controller接口中的handleRequest方法

	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		Map model = new HashMap();
		model.put("helloWorld", getHelloWorld());
		return new ModelAndView(getViewPage(), model);
	}
}








(5)配置Spring文档dispatcherServlet-servlet.xml,放在WEB-INF目录下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> 
   <!-- 定义映射 -->   
    <bean id="urlMapping"       
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">   
          <property name="mappings">  
                    <props>       
                             <prop key="helloWorld.do">helloWorldAction</prop>     
                    </props>  
        
           </property>  
     </bean>  
     
       <!-- 定义视图 -->  
         <bean id="viewResolver"     
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
                <property name="viewClass">  
                          <value>org.springframework.web.servlet.view.InternalResourceView</value>  
                 </property>  
        </bean>   
        
         <!-- 定义控制器 -->  
           <bean id="helloWorldAction"
            class="com.jp.action.HelloWorldAction">   
                 <property name="helloWorld">      
                       <value>Good Luck!</value>   
                 </property> 
                  <property name="viewPage">     
                      <value>/index.jsp</value>     
                   </property>   
             </bean>
             
            </beans>




然后,将项目部署到Tomcat,在浏览器地址栏里输入http://localhost:8080/myApp/helloWorld.do ,即可看到下面的界面


你可能感兴趣的:(spring mvc)