简单介绍下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 如:
配置DispatcherServlet用来对整个请求进行处理同时配置spring所需配置文件
配置参数
注:
接下来配置springmvc拦截请求
表示处理所有以“/”结尾的请求
整个web.xml如下:
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">
接下来开发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
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+"/";
%>
${error }
showAccount.jsp
如:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
账户信息:
卡号:${account.cardNo }
密码:${account.password }
修改applicationContext.xml
如:
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-2.5.xsd">
上述实例完成,如有问题请及时联系
下一篇将学习springmvc+ibatis