spring MVC 模式越来越流行起来, 下面看看一个最简单的例子是怎么完成的:
最终建立完成的例子结构如下图:
下面就分步就一一解释;
1,建立一个J2EE项目;
2,依上图建立相关的包: com.gc.actions, com.gc.models;
3,依上图建立相关的文件夹: (在WebRoot之下)webpage, css[这个仅存CSS文件,可无];
4,增加 spring 支持功能: 右键单击工程名称 > 属性> >myEclipse> add Spring Capabilities
>选spring 3.0版本; 选中 spring 3.0 Core lib, spring 3.0 J2EE lib(自动选中的包保留之); 选复制各个lib包,tld文件到项目的lib下的项
> applicationContext.xml选择存放位置为WEB-INF下> 确定(到这里项目已经有了spring功能);
5,[这一步可不做,最多显示不好而已,不影响运行]在WebRoot下的文件夹css中建立css01.css文件,其内容代码如下:
@CHARSET "UTF-8";
body,input,select,div,td,span{font-family:verdana,arial; font-size:12px;}
.right01{text-align:right}
.right02{float:right;}
.dtime01{font-size:10px; font-family:arial,verdana; color:blue; font-weight:normal;}
.title01{font-size:12px; font-family:verdana,arial; color:blue; font-weight:bold;}
a.a01:link,a.a01:visited{font-size:9px;font-family:arial,verdana;color:blue;font-weight:normal;}
a.a01:active,a.a01:hover{font-size:9px;font-family:arial,verdana;color:blue;font-weight:normal;}
6,在WebRoot下建立index.jsp文件,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>J2EE010 Home Spring MVC实例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/css01.css">
</head>
<body>
J2EE010 Home Spring MVC实例
<hr size="1" color="blue">
<p>
连接: <a href="helloWorld.do">Hello World</a>
</p>
</body>
</html>
7,在WebRoot下的webpage文件夹下建立HelloWorld.jsp文件, 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>利用Spring输出HelloWorld</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/css01.css">
</head>
<body>
利用 Spring 输出HelloWorld
<hr size="1" color="blue">
<%
String str = (String)request.getAttribute("helloWorld");
%>
<br>
<font size="22"><%= str %></font>
<p>
参考: <a href="./">Home page</a>
</p>
</body>
</html>
8, 建立控制器, 在包com.gc.actions上右键单击> New> Class> 输入 HelloWorld> 确定, HelloWorld.java代码类如下:
package com.gc.actions;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloWorld implements Controller { //实现了Controller接口
private String helloWord;
private String viewPage;
//实现Controller接口中的handleRequest()方法
public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
Map <String, String> model=new HashMap<String, String>();
model.put("helloWorld", "Hello World 123456798 output value");
//后一个参数是值,这里直接给出, 实际上这之前进行一个业务处理过程
return new ModelAndView(getViewPage(),model);
}
public String getViewPage() {
return viewPage;
}
public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}
}
9, 双击 applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--定义访问URL和BEAN之间的映射相关,接受请求的helloWorld.do,转入控制器helloWorld-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="helloWorld.do">helloWorld</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>
<!--所有的视图页面都放在根目录的webpage文件夹中-->
<property name="prefix">
<value>/webpage/</value>
</property>
<!--所有视图文件的扩展名都是.jsp-->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!--定义控制器相关: helloWorld-->
<bean id="helloWorld" class="com.gc.actions.HelloWorld">
<property name="viewPage">
<value>HelloWorld</value>
</property>
</bean>
</beans>
10, 双击 web.xml 文件, 其代码应该如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--spring 的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern><!--定义访问类型-->
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
11, 上面开发已经完成, 部署到 Tomcat 服务器中, 并启动服务器,
12, 访问 http://localhost:你的端口号/项目名称/ , 我这里是 http://localhost:9090/J2EE010/ 启动页面,
单击里面的连接即可看到 页面输出 Hello World 123456798 output value 即spring MVC已经工作了, 其他的进行扩展即可得到更多的功能;