第一个Struts2项目-登录Struts

内容提要:本文通过“用户登录”这个简单功能,介绍struts2的标志(Tag)、Action、输入校验(Input Validation)以及本地化输出(Localizing Output)。

开发环境:J2EE+jdk7+tomcat7.0+struts2+junit3.8

项目目录结构:

 

项目工作流程:

1. 显示登录页面等待用户输入

2. 用户输入“用户名”和“密码”点击“登录”

3.在Action类中用户校验得到了执行,如果用户在name/password字段输入admin/admin,

   那么将会显示成功页面.否则页面显示错误信息.

 

开发步骤:

1.创建登陆页面(index.jsp)

代码实现:

 1  <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3     <%@ taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8 <title>Struts2登录界面</title>
 9 </head>
10 <body>
11  <s:form action="LoginAction" namespace="/">
12  <tr>  
13 <td colspan="2" align="center">  Login  </td>  
14  </tr>  
15  <tr>  
16    <td colspan="2">  
17          <s:actionerror />  
18          <s:fielderror />  
19    </td>  
20   </tr>  
21      <s:password name="name" label="用户名"></s:password>
22       <s:textfield name="password" label="密码"></s:textfield>
23       <s:submit  name="submit"  value="登录" ></s:submit> 
24 </s:form>
25 </body>
26 </html>
View Code

 登陆成功页面(main.jsp)

代码实现:

 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html> 
 5 <head>
 6 <title>Login Success</title>
 7 </head>
 8 <body>
 9 <p align="center"><font color="#000080" size="5">Login Successful</font></p>
10 </body>
11 </html>
View Code

其中代码 <s:actionerror />和<s:fielderror />是Struts2标签库里的通用标签。用来显示Action和字段校验的错误,先不用管。

2.配置web.xml

代码实现:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>Struts</display-name>
 4   
 5   
 6   <filter>
 7         <filter-name>struts2</filter-name>
 8         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 9  </filter>
10  <filter-mapping>
11         <filter-name>struts2</filter-name>
12         <url-pattern>/*</url-pattern>
13  </filter-mapping>
14     
15  <welcome-file-list>
16     <welcome-file>index.jsp</welcome-file>
17    
18   </welcome-file-list>
19 </web-app>
View Code

 

3.创建LoginAction类

现在让我们创建LoginAction类来处理登录请求.在Struts2中不一定非要实现Action接口,任何含有execute()方法的POJO都可以当作Action。

使用.Struts2提供了一个基础的ActionSupport类来实现常用的接口.在我们的Action类中(LoginAction.java)我们实现了ActionSupport接口.

我们的"LoginAction.java"存放在“package com.lizanhong.action” 

代码实现:

 1 package com.lizanhong.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class LoginAction extends ActionSupport {
 6     private static final long serialVersionUID = 1L;
 7     
 8     private String name;
 9     private String password;
10     
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getPassword() {
18         return password;
19     }
20     public void setPassword(String password) {
21         this.password = password;
22     }
23     
24     
25     public String execute(){
26         System.out.println("Validating login");
27 //        获取表单中的姓名
28         String name = this.getName();
29 //        获取表单中的密码
30         String password = this.getPassword();
31 //        判断用户名及密码是否正确
32         if(!name.equals("admin") || !password.equals("admin")){  
33             addActionError("Invalid user name or password! Please try again!");  
34             return ERROR;  
35     }else{  
36       return SUCCESS;  
37     }  
38     }
39     
40 }
View Code

 

4.配置LoginAction映射(在struts.xml)

代码实现:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <package name="default" namespace="/" extends="struts-default">
 7         <action name="LoginAction" class="com.lizanhong.action.LoginAction">
 8             <!-- 定义逻辑视图和物理资源之间的映射 -->
 9             <result name="error">index.jsp</result>
10             <result >/WEB-INF/main.jsp</result>
11         </action>
12     </package>
13 </struts>
View Code

 

  这个项目是自己亲自实践的,有问题欢迎大家讨论。下一篇博文我会添加校验功能。

 

转载于:https://www.cnblogs.com/moonstar/p/3603677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值