Struts2 登录示例

Struts2 登录示例
2013-05-20       0  个评论       作者:被动
收藏     我要投稿

(1)创建一个Web项目工程Login,添加Struts2所需的所有JAR文件,然后配置文件struts.xml。该文件放置到src目录下,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" />
 
 <package name="default" extends="struts-default">
  <action name="LoginAction" class="cn.com.struts2.action.LoginAction">
  <result name="success">/success.jsp</result>
  <result name="input">/login.jsp</result>
  </action>
 </package>
</struts>

(2)创建登录页面Login.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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=ISO-8859-1">
<title>My JSP 'index.jsp' starting page</title>
<s:head theme="ajax" />
</head>
<body>
 <s:form action="LoginAction" method="post">
  <s:label value="登录"></s:label>
  <s:textfield name="account" label="请输入用户名"></s:textfield>
  <s:password name="password" label="请输入密码"></s:password>
  <s:submit value="登录" ></s:submit>
 </s:form>
</body>
</html>

(3)登录后的跳转页面为success.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts" %>
<!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=UTF-8">
<title>My JSP 'index.jsp' starting page</title>
<struts:head theme="ajax" />
</head>
<body>
 登录成功,欢迎你,<struts:property  value="account" />
</body>
</html>

在以上两个页面中使用了Struts2标签,其使用时只需要在页面开头部分添加如下语句:

<%@ taglib uri="/struts-tags" prefix="struts" %>

(4)创建了登录页面和登录成功后的页面后,还需要设置Action,项目中的Action为LoginAction.java,代码如下:

 

package cn.com.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String account;
 private String password;
 
 public String execute() {
  if("123456".equalsIgnoreCase(account)&&"123456".equals(password))
  {
   return SUCCESS;
  }
  return INPUT;
 }
 
 public String getAccount()
 {
  return account;
 }
 
 public void setAccount(String account)
 {
  this.account = account;
 }
 
 public String getPassword()
 {
  return password;
 }
 
 public void setPassword(String password)
 {
  this.password =password;
 }

/* @Override
 public void validate() {
  // TODO Auto-generated method stub
  if(account==null||"".equals(account))
  {
   this.addFieldError("account", "用户名不能为空!");
  }
  else if(account.length()!=6)
  {
   this.addFieldError("account","用户名必须为6位");
  }
 
  if(password==null||"".equals(password))
  {
   this.addFieldError("password","密码不能为空!");
  }
  else if(password.length()<6||password.length()>12)
  {
   this.addFieldError("password","密码长度必须在6到12位之间!");
  }
 }
 
 */
}
(5)在Action中设置了验证的登录用户名为“action”,密码为“1234”,如果登录的用户名和密码和设置的用户名密码匹配,则跳转到success.jsp。最后需要在配置文件web.xml中进行配置,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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
<filter>
 <filter-name>struts2</filter-name>
 <filter-class>
  org.apache.struts2.dispatcher.FilterDispatcher
 </filter-class>
</filter>
<filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
 <welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

 


你可能感兴趣的:(Struts2 登录示例)