简单介绍下SpringMVC构成

构成SpringMVC的主要组件如下

1、DispatcherServlet

2、HandlerMapping

3、Controller

4、ViewResolver

 

了解SpringMVC的构成之后说下SpringMvc的处理流程顺便说下每部分都负责做什么

spring mvc处理过程
1、客户端请求提交到DispatcherServlet
2、DispatcherServlet查询一个或多个HandlerMapping,找到处理请求的Controller
3、DispatcherServlet将请求提交到Controller
4、Controller调用业务逻辑处理后,返回ModelAndView对象
5、DispatcherServlet查询一个或多个ViewResolver视图解析器,找到ModelAndView指定的视图
6、视图负责将结果展示给客户端

 

用实例来说明SpringMVC的开发步骤

1、新建web project

2、添加spring支持
3、修改web.xml,配置DispatcherServlet
4、实现Model层
5、实现Controller
6、实现View层
7、修改applicationContext.xml

 

实际操作步骤

1、打开myeclipse创建web project
2、创建好工程后构建路径添加所需jar
工程上点击鼠标右键--选择myeclipse--选择Add Spring Capabilities
--勾选MyEclipse Libraries--勾选spring3.0核心包(Spring 3.0 Core Libraries)由于是spring mvc项目 勾选Spring 3.0 Web Libraries

3、点击确定,工程建立完毕

4、修改web.xml

添加 DispatcherServlet 如:

<servlet-name>Dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

配置DispatcherServlet用来对整个请求进行处理同时配置spring所需配置文件

配置参数<init-param>

<param-name>namespace</param-name>

<param-value>springmvc-servlet</param-value>

<!-- spring 配置文件,名称的路径都可以配置,如果不配置此项则自动扫描/WEB-INF/ springmvc-servlet.xml,此文件名规则为servlet名-servlet.xml -->

 

注:<!-- 加载多个配置文件 <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/applicationContext_*.xml, /WEB-INF/conf/user-servlet.xml </param-value> -->

 

<!--servlet加载方式,启动容器时加载-->

<load-on-startup>1</load-on-startup>

 

接下来配置springmvc拦截请求

<servlet-mapping>

<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

表示处理所有以“/”结尾的请求

 

整个web.xml如下:

 

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

<web-app version="3.0"
xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
">
<display-name></display-name>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

 

接下来开发model如下:

package com.sinosig.model;

public class Account {

private String cardNo;
private String password;
private float balance;
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
}

下一步:

开发controller

新建LoginController 继承AbstractController 相关业务逻辑在此Controller中进行封装

如:

package com.sinosig.web.controller;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractController;

import com.sinosig.model.Account;

public class LoginController extends AbstractController {

private String formView;
private String successVies;
public String getFormView() {
return formView;
}

public void setFormView(String formView) {

this.formView = formView;
}

public String getSuccessVies() {

return successVies;
}

public void setSuccessVies(String successVies) {

this.successVies = successVies;
}

@Override

protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 获取账户和密码
String cardNo = request.getParameter("cardNo");
String password = request.getParameter("password");
Account account = getAccount(cardNo, password);
Map<String ,Object> model = new HashMap<String, Object>();
if (account != null) {
model.put("account", account);
return new ModelAndView(getSuccessVies(), model);
} else {
model.put("error", "卡号和密码不正确");
return new ModelAndView(getFormView(), model);
}
}

public Account getAccount(String cardNo, String password){

if (cardNo.equals("123") && password.equals("123")) {
Account account = new Account();
account.setCardNo(cardNo);
account.setBalance(88.8f);
return account;
} else {
return null;
}
}
}

 

开发login.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>My JSP 'login.jsp' starting page</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="styles.css">
-->

</head>

<body>
${error }
<form method="post" action="login.do">
<table>
<tr><td colspan="2" align="center">账户登录</td></tr>
<tr>
<td>卡号:</td>
<td><input type="text" name="cardNo" value=""></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value="提交">
<input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>

 

showAccount.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>My JSP 'showAccount.jsp' starting page</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="styles.css">
-->

</head>

<body>
账户信息:<br/>
卡号:${account.cardNo }
密码:${account.password }
</body>
</html>

修改applicationContext.xml

 

如:

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

<beans
xmlns=""
xmlns:xsi=""
xmlns:p=""
xsi:schemaLocation=" ">
<!-- 配置请求的controller的业务类 -->
<bean id="loginController" class="com.sinosig.web.controller.LoginController">
<property name="formView" value="login"></property>
<property name="successVies" value="showAccount"></property>
</bean>
<!-- 配置请controller和URL地址的映射关系 -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">loginController</prop>
</props>
</property>
</bean>
<!-- 配置jsp作为试图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

 

上述实例完成,如有问题请及时联系

下一篇将学习springmvc+ibatis