分类:
2008-10-15 13:46:34
在JSP 的各种Web框架中,据我看来Struts框架最为简单。下面从Struts框架开始来研究JSP的web框架。
1、MVC
在研究这个框架之前,先要对MVC模式有所了解。幸好以前学习过设计模式,对MVC编程和模式了解一些,即将模型、视图、控制器分开。使三者可以各负其责,处理自己的逻辑。
2、Struts框架的介绍:
Struts框架也采用了MVC结构:
Model:在Struts框架中,Model模型的实现是Bean.在Struts框架中,通过使用Bean完成信息的。
Control:控制器即Java Servlet,由框架提供,负责视图和模型之间的交互。控制器的每个入口都由名为struts-config.xml的配置文件设置。该文件把来自视图的请求映射为特定的Java类
以进行相应的处理,控制器还指定下一个视图的位置。
View:视图,当然使用JSP了。
是不是很简单啊。
下面的序列图反映了一个简单的Strut构架的运行过程:
看到上面的序列图,如果让我们从头来写代码,来实现Struts构架,这样也太累了。Struts构架给我们提供了不少类,如果ActionServlet、Action、ActionForm等。通过继承使用他们和使用框架,我们只需写几个继承类,就可以写出基于这个框架的代码了。这就是框架的好处,他把一些细节给我们屏蔽了,我们只要实现了关键部分。OK,就可以了。结构又好,又方便。这就是框架的好处。
通过上面的说明,或许你对框架会有些了解。框架,简单的理解,就是我们小学时做的填空题,我们把重要的空填一下。就可以了。显然,上面的描述不是很准确,但用来简单的理解框架还是可以的。
下面需要用一个实例来说明如何使用struts框架了。
我们用Struts实现一个简单的注册信息页面。
需要注册的信息只由用户名和密码。下面是具体的实现。
首先定义一个User类:
1: public class User {
2: private String userName;
3: private String password;
4:
5: public String getPassword() {
6: return password;
7: }
8:
9: public void setPassword(String password) {
10: this.password = password;
11: }
12:
13: public String getUserName() {
14: return userName;
15: }
16:
17: public void setUserName(String userName) {
18: this.userName = userName;
19: }
20:
21: }
public class UserForm extends ActionForm {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getUserName() {
return user.getUserName();
}
public String getPassword() {
return user.getPassword();
}
@Override
public void reset(ActionMapping a, HttpServletRequest r) {
this.user = new User();
}
@Override
public ActionErrors validate(ActionMapping a, HttpServletRequest r) {
ActionErrors errors = new ActionErrors();
if(user.getUserName()==null||(user.getPassword()==null)){
errors.add("请填写用户名和密码!",new ActionMessage("error.user.password"));
}
return errors;
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
*
* @author WangJing
*/
public class UserAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm arg1, HttpServletRequest request, HttpServletResponse arg3) throws Exception {
UserForm userForm = new UserForm();
try{
UserBean bean = new UserBean();
bean.addUser(userForm.getUser());
}catch(Exception e){
e.printStackTrace();
}
request.setAttribute("user",userForm.getUser());
return (mapping.findForward("userCreated"));
}
}
class UserBean {
void addUser(User user) {
throw new UnsupportedOperationException("Not yet implemented");
}private Connection conn;
public UserBean() {
this.conn = DatabaseConn.getConnection();
}
public void addUser(User user){
Statement s = (Statement) conn.createStatement();
s.executeUpdate("insert into User values("+user.getUserName()+","+user.getPassword()+");");
}
}
上面的代码进行了数据的存储处理。需要注意的是代码中的DatabaseConn.getConnection()是没有的。需要自己实现的。具体的实现可以参考本人的博客中关于连接数据库的一篇文章。
好了,下面就是最重要的显示JSP文件代码的编写了:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Pagetitle>
head>
<body>
<html:form aciton="createUser.do" method="GET">
userName:<html:text property="user.userName"/>
password:<html:text property="user.password"/>
<html:submit property="submit"/>
html:form>
body>
html>
下面用viewUser.jsp来作为响应页面:
JSP Page
<%
User user= (User)request.getAttribute("User");%>
已经创建了用户:
name:<%=user.getUserName()%>
password:<%=user.getPassword()%>
--------------------next---------------------public class UserForm extends ActionForm {private User user;
public User getUser() {
return user;
}public void setUser(User user) {this.user = user;
}public String getUserName() {
return user.getUserName();
}public String getPassword() {
return user.getPassword();
}@Overridepublic void reset(ActionMapping a, HttpServletRequest r) {this.user = new User();}@Overridepublic ActionErrors validate(ActionMapping a, HttpServletRequest r) {
ActionErrors errors = new ActionErrors();
if(user.getUserName()==null||(user.getPassword()==null)){errors.add("请填写用户名和密码!",new ActionMessage("error.user.password"));}return errors;
}}
--------------------next---------------------import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;/**
*
* @author WangJing
*/
public class UserAction extends Action {@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm arg1, HttpServletRequest request, HttpServletResponse arg3) throws Exception {
UserForm userForm = new UserForm();
try{
UserBean bean = new UserBean();
bean.addUser(userForm.getUser());}catch(Exception e){
e.printStackTrace();}request.setAttribute("user",userForm.getUser());
return (mapping.findForward("userCreated"));}}
--------------------next---------------------class UserBean {
void addUser(User user) {
throw new UnsupportedOperationException("Not yet implemented");
}private Connection conn;
public UserBean() {
this.conn = DatabaseConn.getConnection();
}
public void addUser(User user){
Statement s = (Statement) conn.createStatement();
s.executeUpdate("insert into User values("+user.getUserName()+","+user.getPassword()+");");
}
}
--------------------next---------------------<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
""><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Pagetitle>head><body><html:form aciton="createUser.do" method="GET">userName:<html:text property="user.userName"/>password:<html:text property="user.password"/><html:submit property="submit"/>html:form>body>html>
--------------------next---------------------
阅读(389) | 评论(0) | 转发(0) |