从上篇博客能够看到,事实上Struts1框架就是封装了一些页面的转向、数据类型的转换,去除冗余的if else推断。除了这些,事实上还封装了一些我们寻经常使用的JSTL标签库,文件上传等等。

       以下看怎样运用Strut是实现登陆和加入用户实例。

项目文件夹结构:

                 

Login.jsp。登录页面

<%@ page language="java" import="java.util.*" pageEncoding="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 'MyJsp.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">
  </head>
  
  <body>
  	<form action="Login.do?

command=Login" method="post"> 姓名:<input type="text" name="username"><br> 密码:<input type="password" name="password"></br> <input type="submit" value="登录"> </form> </body> </html>

user_input.jsp 加入页面

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
		<title>加入用户</title>
		<link rel="stylesheet" href="../style/drp.css">
		<script type="text/javascript">

	function addUser() {
		with (document.getElementById("userForm")) {
			method = "post";
			action = "Login.do?command=Add";
			submit();
		}
	}
</script>
	</head>
	<body class="body1">
		<form name="userForm" target="_self" id="userForm">
			<div align="center">
				<table width="95%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td width="22%" height="29">
							<div align="right">用户代码: </div>
						</td>
						<td width="78%">
							<input name="userId" type="text" class="text1" id="userId"
								size="10" maxlength="10">
						</td>
					</tr>
					<tr>
						<td height="26">
							<div align="right">用户名称: </div>
						</td>
						<td>
							<input name="username" type="text" class="text1" id="userName"
								size="20" maxlength="20">
						</td>
					</tr>
					<tr>
						<td height="26">
							<div align="right">密码: </div>
						</td>
						<td>
								<input name="password" type="password" class="text1"
									id="password" size="20" maxlength="20">
						</td>
					</tr>
					<tr>
						<td height="26">
							<div align="right">联系电话: </div>
						</td>
						<td>
							<input name="contactTel" type="text" class="text1"
								id="contactTel" size="20" maxlength="20">
						</td>
					</tr>
					<tr>
						<td height="26">
							<div align="right">email: </div>
						</td>
						<td>
							<input name="email" type="text" class="text1" id="email"
								size="20" maxlength="20">
						</td>
					</tr>
				</table>
				<div align="center">
					<input name="btnAdd" class="button1" type="button" id="btnAdd"
						value="加入" onClick="addUser()">
				</div>
			</div>
		</form>
	</body>
</html>

struts-config.xml:

         配置ActionForm, ActionBean,异常的转向,国际化资源文件的位置,插件(便于和Spring集成)等

<?

xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="loginactionform" type="userloginpackage.loginactionform"></form-bean> </form-beans> <action-mappings> <action path="/Login" type="userloginpackage.loginaction" name="loginactionform" scope="request" parameter="command" > <forward name="success" path="/Addsuccess.jsp"></forward> <forward name="error" path="/loginerror.jsp"></forward> <forward name="LoginToAdd" path="/user_input.jsp"></forward> </action> </action-mappings> </struts-config>

UserManager类:

       与数据库打交道,实现增删改查操作。

package userloginpackage;
public class UserManager {
	public User Login(User user){
		return user;
	}
	public String Add(User user ) {
		return ("UserManager.Add");
	}
}

LoginAction类:

        运行详细业务操作。接收ActionForm对象。接收表单属性參数。一个Action相当于Servlet,从ActionForm中取得相关用户输入參数后,保存在request范围中,使用ActionMappting中的findForward方法进行跳转。

package userloginpackage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class loginaction extends DispatchAction {

	public ActionForward Login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		loginactionform fo=(loginactionform)form;
		UserManager userManager=new UserManager();
		User user=new User();
		user.setUserName(fo.getUsername());
		user.setPassword(fo.getPassword());
		User Flag=userManager.Login(user);
		if(fo.getUsername().equals(Flag.getUserName()) && fo.getPassword().equals(Flag.getPassword())){
			return mapping.findForward("LoginToAdd");
		}else{
			return mapping.findForward("error");
		}
	}
	
	public ActionForward Add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		loginactionform fo=(loginactionform)form;
		String flag="";
		UserManager userManager=new UserManager();
		User user=new User();
		loginactionform laf=(loginactionform)form;
		BeanUtils.copyProperties(user, laf);
		flag=userManager.Add(user);
		if("UserManager.Add".equals(flag)){
			return mapping.findForward("success");
		}else{
			return mapping.findForward("error");
		}
	}

}

loginactionform类:

        封装属性參数与表单中的參数一致,提供getter、setter方法,进行业务验证等。

package userloginpackage;

import org.apache.struts.action.ActionForm;

public class loginactionform extends ActionForm {
	private String username;
	private String password;
	private Integer userId;
	private String contactTel;
	private String email;
	
	public String getUsername() {
		return username;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getContactTel() {
		return contactTel;
	}
	public void setContactTel(String contactTel) {
		this.contactTel = contactTel;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}		
}

web.xml:

      程序启动首先读取的xml,是web程序入口,定义struts-config.xml。

<?

xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

       另外再加一个User实体类。这样,就完毕了一个利用Struts框架,完毕了功能实现。

尽管如此。Struts框架与原始MVC模式有什么差别,Struts框架下,整个执行过程又是怎样的。我们应该怎样理解Struts的实现理念,看下一篇博客吧。。。

。。







        

posted on 2017-08-07 15:41  yutingliuyl  阅读(253)  评论(0编辑  收藏  举报