最近学习Struts的框架,就跟着视频,写了一个简单的Struts框架。
在Myeclipse中创建一个自己的Web项目:
我们,将包引入完成之后,我们就开始写我们的界面。写一个引入界面:Index.jsp:
<span style="font-size:18px;"><%-- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> --%> <%@ page contentType="text/html;charset=GB18030" %> <% 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 'index.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> <a href="login.jsp">登录</a> </body> </html> </span>
<span style="font-size:18px;"><%-- <%@ page language="java" contentType="text/html; charset=GB18030" --%> <%-- pageEncoding="GB18030"%> --%> <%@ page contentType="text/html;charset=GB18030" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <form action="login.do" method="post"> 用户:<input type="text" name="username"><br> 密码:<input type="password" name="password"></br> <input type="submit" value="登录"> </form> </body> </html></span>
<span style="font-size:18px;"><%-- <%@ page language="java" contentType="text/html; charset=GB18030" --%> <%-- pageEncoding="GB18030"%> --%> <%@ page contentType="text/html;charset=GB18030" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <%-- <%=request.getAttribute("msg") %> --%> ${msg } </body> </html></span>
<span style="font-size:18px;"><%-- <%@ page language="java" contentType="text/html; charset=GB18030" --%> <%-- pageEncoding="GB18030"%> --%> <%@ page contentType="text/html;charset=GB18030" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> ${loginForm.username },登录成功 </body> </html></span>
目录结构:
我们写ActionForm类这个相当于Model:
<span style="font-size:18px;">package com.bjpowernode.struts; import org.apache.struts.action.ActionForm; /** * 登录ActionForm,负责表单收集数据 * 表单的属性必须和ActionForm中的get和set的属性一致 * @author Administrator * */ @SuppressWarnings("serial") public class LoginActionForm extends ActionForm { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } </span>
写我们的业务代码UserManager:
<span style="font-size:18px;">package com.bjpowernode.struts; public class UserManager { public void login(String username, String password) { if (!"admin".equals(username)) { throw new UserNotFoundException(); } if (!"admin".equals(password)) { throw new PasswordErrorException(); } } } </span>
<span style="font-size:18px;">package com.bjpowernode.struts; public class PasswordErrorException extends RuntimeException { public PasswordErrorException() { // TODO Auto-generated constructor stub } public PasswordErrorException(String message) { super(message); // TODO Auto-generated constructor stub } public PasswordErrorException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public PasswordErrorException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } } </span>
<span style="font-size:18px;">package com.bjpowernode.struts; public class PasswordErrorException extends RuntimeException { public PasswordErrorException() { // TODO Auto-generated constructor stub } public PasswordErrorException(String message) { super(message); // TODO Auto-generated constructor stub } public PasswordErrorException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public PasswordErrorException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } } </span>
重点来了,我们写Action:LoginAction
<span style="font-size:18px;">package com.bjpowernode.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * 登录Action * 负责取得表单数据、调用业务逻辑、返回转向信息 * * @author Administrator * */ public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf = (LoginActionForm)form; String username = laf.getUsername(); String password = laf.getPassword(); UserManager userManager = new UserManager(); try { userManager.login(username, password); return mapping.findForward("success"); }catch(UserNotFoundException e) { e.printStackTrace(); request.setAttribute("msg", "用户不能找到,用户名称=【" + username + "】"); }catch(PasswordErrorException e) { e.printStackTrace(); request.setAttribute("msg", "密码错误"); } return mapping.findForward("error"); } } </span>
最重要的一步,就是在配置文件中配置我们Struts文件了:struts-config.xml:
<span style="font-size:18px;"><?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/> </form-beans> <action-mappings> <action path="/login" type="com.bjpowernode.struts.LoginAction" name="loginForm" scope="request" > <forward name="success" path="/login_success.jsp" /> <forward name="error" path="/login_error.jsp"/> </action> </action-mappings> </struts-config> </span>
在这里我们说一下我们最重要的配置文件中的标签:
form-beans:该标签用查找使用的ActionForm,ActionForm相当于Model,可以被其他bean或者过程作为数据载体,具有数据传输的作用。我们在界面上用到的:${loginForm.username},就是在使用了ActionForm。
action-mapping:该标签是用来存储关联界面的,我们看login.jsp中我们是这样写的:<form action="login.do" method="post">,这里的login.do,会去Struts.xml中进行配置,我们就可以根据type的值,去访LoginAction中的方法:execte。
action-forword:该标签用来转向界面的。<forward name="success" path="/login_success.jsp" />,而我们在LoginAction中使用的:return mapping.findForward("success");正好对应就可以了。
用一张图来表示我们这个框架: