MVC架构探究及其源码实现(6)-简单示例
博学,切问,近思--詹子知 (https://jameszhan.github.io)
在前一系列的文章中,我们已经完成了MVC架构模式的简单实现,尽管有些粗糙,有些功能还不完善,但是,麻雀虽小,五脏俱全。我们现在就用这个小小的框架,来实现我们的几个简单的应用。
限于篇幅,我们不可能把应用的所有代码都贴上来,我们先来演示一个Hello World的简单应用。
实现控制器HelloController.javapackage com.google.mvc.web.sample;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.google.mvc.web.servlet.ModelAndView;
import com.google.mvc.web.servlet.mvc.Controller;
public class HelloController implements Controller {
private static final Logger LOGGER = Logger.getLogger(HelloController.class);
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String now = (new Date()).toString();
LOGGER.info("Returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}
我们再来看看具体的配置(mvc.config.xml)<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean />
<bean />
<bean >
<property name="viewClass" value="com.google.mvc.web.servlet.mvc.InternalResourceView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean />
<bean />
</beans>
相应的View文件hello.jsp的实现<html>
<head>
<title>Hello World</title>
</head>
<body>
<strong>Current time is ${now}</strong>
</body>
</html> 根据我们之前的配置,我们应把此文件放到WEB-INF目录下 。
到现在为止,我们简单的HelloWorld的应用就已经完成了。部署完成后,我们访问application下的hello.do便可以访问此页面,并且可以看到当前时间的显示。是不是很简单呀。
这个应用确实是太简单了,以至于我们无法看出MVC的任何强大之处。下面我们再看一个稍微复杂一点的例子,用户登录的验证。
首先,我们来实现我们的控制器LoginController.java package com.google.mvc.web.sample;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.google.mvc.web.servlet.ModelAndView;
import com.google.mvc.web.servlet.mvc.Controller;
public class LoginController implements Controller
{
private static final Logger LOGGER = Logger.getLogger(LoginController.class);
private static final Map<String, String> MAP = new HashMap<String, String>();
static{
MAP.put("james", "123456");
MAP.put("andy", "123456");
MAP.put("link", "123456");
MAP.put("sample", "123456");
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String username = request.getParameter("username");
String password = request.getParameter("password");
if(LOGGER.isDebugEnabled()){
LOGGER.debug("username = " + username);
LOGGER.debug("password = " + password);
}
if(username == null || password == null){
return new ModelAndView("login");
}
username = username.trim();
password = password.trim();
String errMessage = null;
if(username.equals("") || password.equals("")){
errMessage = "User name or password can't be empty!";
}else{
String pass = MAP.get(username);
if(pass == null){
errMessage = "User doesn't exist!";
}else if(!password.equals(pass)){
errMessage = "Wrong password!";
}
}
if(errMessage == null){
return new ModelAndView("success", "username", username);
}else{
return new ModelAndView("failure", "errMessage", errMessage);
}
}
}
为了简单起见,我们使用Map来维护用户的基本信息。同样,我们需要把我们的控制器配置到我们的系统当中,参照前一个示例的配置,我们只需要在mvc.config.xml中加入下面的一行代码即可。<bean /> 因为登陆过程中,可能会遇到不同的情况,我们需要为不同的状态准备不同的视图。
1.默认登录页面实现(login.jsp):<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Login Page</title> </head> <body> <table width="400" border="1" align="center"> <form action="login.do" method="post" enctype="application/x-www-form-urlencoded"> <tr> <td>User Name:</td> <td><input name="username" type="text" /></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password" /></td> </tr> <tr> <td colspan="2"><button type="submit">Submit</button></td> </tr> </form> </table> </body> </html> 2.登陆成功页面实现(success.jsp):<b>Welcome, ${username}</b> 3.登陆失败页面的实现(failure.jsp):<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Login Page</title> </head> <body> <form action="login.do" method="post" enctype="application/x-www-form-urlencoded"> <table width="400" border="1" align="center"> <tr> <td>User Name:</td> <td><input name="username" type="text" /></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password" /></td> </tr> <tr> <td colspan="2"><button type="submit">Submit</button></td> </tr> <tr> <td colspan="2" mce_>${errMessage}</td> </tr> </table> </form> </body> </html> 到这里,我们的登陆功能已经基本完成了。部署好应用后,你就可以通过访问login.do来测试我们这个应用的功能了。
尽管我们的MVC框架功能比较简单,但是扩展能力还是很强的,因为我们的目地是为了通过源码实现去更好地掌握MVC框架的实现原理,所以我们将不对此框架进行优化,毕竟,现在成熟而优秀的MVC框架已经有很多了。
相关文章:
- MVC架构探究及其源码实现(1)-理论基础
- MVC架构探究及其源码实现(2)-核心组件定义
- MVC架构探究及其源码实现(3)-WebApplicationContext
- MVC架构探究及其源码实现(4)-前端控制器
- MVC架构探究及其源码实现(5)-相关组件实现
最后更新:2017-04-02 04:01:42