使用Struts分层web框架,实现简单的用户登录

struts框架具有组件的模块化,灵活性和重用性的优点,同时简化了基于MVC的web应用程序的开发。他可以清晰地区分控制,事务逻辑和外观,从而简化了开发应用程序的过程,使用Struts的目的是为了减少在运用MVC设计模型来开发Web应用的时间。

一、新建WEB项目app,右键点击项目,选择MyEclipse—Add Struts Capabilities,选择Struts 2.1,这一步完成之后,项目会有三个变化,如下图

                       

二、开始写struts部分代码

1.首先要创建一个Action

步骤:点击src下的包--->右键——>class——>取名LoginAction——>点击Browse——>继承ActionSupport。首先需要定义它的两个属性userID、password,并生成getter and setter方法。打开LoginAction后鼠标右键——>source——> @Override/Implement Method——>重写execute()方法

在这里自定义用户ID为 "admin" 用户密码为 “123” 具体如下:

package struts;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private String userName,password;
	@Override
	public String execute() throws Exception {
		if(userName.equals("aaa")&&password.equals("123"))
			return "success";
		else return "error";
	}
	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;
	}

}

2.配置struts.xml文件,主要注意action的name,class相对应的名字,以及result 的执行结果(name *)

<struts>
	<package name="default" extends="struts-default">
		<!-- 用户登录 -->
		<action name="userLogin" class="struts.LoginAction">
			<result name="success">/main.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

3.再写 index.jsp代码,客户端需要输入用户名及密码,传给C层 ,以便C做出相应的处理

body部分 代码如下:访问服务器是需要手动输入相应的用户ID及密码

<body>
 <form action="userLogin.action" method="post">
  用户名:<input type="text" name="userName" 用户名><br>
  密码:<input type="password" name="password" ><br>
  <input type="submit" value="登录"><br></center>
  </body>

4.由上可知result的执行结果还有两个界面(success.jsp、false.jsp)

    4.1 success.jsp 注意要将struts标签引入,否则该标签不会生效

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'main.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>
  登录成功,欢迎<s:property value="userName"></s:property>进入主页 <br>
  </body>
</html>

 4.2 error.jsp  此页面只是显示登录失败的信息,所以只需一句提示语即可 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'error.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>
    登录失败 <br>
  </body>
</html>

 三、运行,调试

1.将该项目部署在Tomcat服务器上,开启服务,运行此项目,此时显示出登录界面,有用户ID和密码连个编辑框,需要手动输入相应的值。

当我输入用户ID为 “aa”,密码为“123” ,action将识别我输入的用户ID及密码是否正确,此时我的用户ID是错误的,结果显示登录失败。

 再次退回 login.jsp,输入正确的用户ID及密码时,即可登录成功。

 四、总结,体会

使用Struts的目的是为了减少在运用MVC设计模型来开发Web应用的时间,容易忽略掉的是:

1.struts的Jar包没有导入;

2.相应的sruts.xml文件没有配置正确;

3.action 的name值尽量不要用敏感词汇,比如:login、bean等,不然很容易出错

4.地址栏访问的页面要符合逻辑,要注意斟酌struts的工作原理。

原文地址:https://www.cnblogs.com/Believer/p/5353930.html