MyEclipse2014环境下搭建struts2框架并实现简单的登录验证

前置条件

			1.安装好JDK并配置好环境变量
			2.安装好myEclipse2014或其它版本

搭建步骤

  1. 打开myEclipse2014->选择文件->新建->web project。
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  2. 输入项目名(假设我的项目名为StrutsDemo,建议初学者取得项目名和我的一样,因为后面也会用到,熟悉后在自行操作)并选择版本->单击下一步。
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  3. 选择如图选项单击下一步。
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  4. 选择如图选项单击完成
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  5. 单击选中项目->右键->MyEclipse->add Struts;有的读者可能没有这个选项,比如本人就没有,这可按以下操作。
    单击选中项目->右键->MyEclipse->Project facets->Install Apache Struts(2.x) Facet;
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  6. 选择如图选项,单击下一步。
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  7. 选择如图选项单击下一步
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  8. 在项目WebRoot目录下创建一下三个jsp文件
    login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>图书管理系统</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>
	<s:form action="login" method="post" theme="simple">
			登录名:<s:textfield name="loginName" seze="20"/><br>
			登录名:<s:password name="password" seze="20"/><br>
			<s:submit value="登录"/>
			<s:reset value="重置"/>
	</s:form>
  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 first javaEE</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>
    success!<br> 
  </body>
</html>

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>登录失败</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>
    error! <br>
  </body>
</html>

9.在项目的src文件夹下新建名为action的包,并在包下新建LoginAction类。

新建包:单击选中项目,右键->新建->包->输入包名确定即可。
新建类:单击选中action文件夹,右键->新建->类->输入类名确定即可。

LoginAction代码:

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
	/**用户名*/
	private String loginName;
	/**密码*/
	private String passWord;
	public String execute()
	{
		if(loginName.equals("dd") && passWord.equals("123")) return "success";
		else return "error";
	}
	public String getLoginName()
	{
		return loginName;
	}
	public void setLoginName(String loginName)
	{
		this.loginName = loginName;
	}
	public String getPassWord()
	{
		return passWord;
	}
	public void setPassword(String passWord)
	{
		this.passWord = passWord;
	}
}


注意:LoginAction类中的loginName属性和passWord属性必须和login.jsp中name="loginName"和name="password"
的名字完全一样,读者直接复制粘贴即可。


 类中代码  if(loginName.equals("dd") && passWord.equals("123")) return "success";
		else return "error";表示设置的验证用户名为dd,密码为123;
  1. 配置web.xml
    在工程WebRoot/WEB-INF目录下打开web.xml文件并修改为如下代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>strutsDemo</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
  1. 在工程src文件夹下打开struts.xml文件(若没有这在此文件夹下新建),并修改为一下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="user" namespace="/" extends="struts-default">
		<action name="login" class="action.LoginAction">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>    

  1. 保存所有刚才做的一切,最高刷新一下项目开始测试。
    打开login.jsp,Ctrl+F11,选中如图选项单击确定
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
    选中如图选项单击确定
    MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
  2. 在输入框中输入以下信息
 用户名:dd
 密码:123
 这是正确的用户名和密码,也就是刚才在类LoginAction中设置的

MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
14.单击确定,显示结果如图表示成功
MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
15.返回测试一下用户名或者密码不正确的情况为如图MyEclipse2014环境下搭建struts2框架并实现简单的登录验证
16.到此,一个简单的struts2框架就算是搭建好了。此时的目录结构如下:
MyEclipse2014环境下搭建struts2框架并实现简单的登录验证