struts2学习笔记二(第2讲.Struts2入门与配置续)
程序员文章站
2022-05-28 15:53:23
...
接上面未完成的功能:判断输入的用户名和密码是否正确。
一、创建一个新的登录页面:login2.jsp(struts2的标签库的描述符放在struts2-core-2.0.14.jar下的META-INF/struts-tags.tld中)
二、编辑LoginAction.java类,使其继承自ActionSupport类,并且继承一个validate方法,用于验证用户名和密码输入是否为空或者是否正确,LoginAction.java类代码最终如下:
三、编辑配置struts.xml,对于出现不同错误指向不同的位置:
图片演示操作的效果:
都为空时:
[img]http://dl.iteye.com/upload/attachment/382617/c343f2bb-d33e-3497-9291-e35a2a150a9e.jpg[/img]
用户名为空时:
[img]http://dl.iteye.com/upload/attachment/382619/2a5e84d4-b35c-33fb-bc30-08b82f106075.jpg[/img]
密码为空时:
[img]http://dl.iteye.com/upload/attachment/382621/86973a77-a687-3348-ac48-1845937e800e.jpg[/img]
都正确时:
[img]http://dl.iteye.com/upload/attachment/382623/8ec2b7bc-417d-355e-8c7b-135e2f02a8a4.jpg[/img]
一、创建一个新的登录页面:login2.jsp(struts2的标签库的描述符放在struts2-core-2.0.14.jar下的META-INF/struts-tags.tld中)
<%@ page language="java" contentType="text/html" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login2.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="sumbit"></s:submit>
</s:form>
</body>
</html>
二、编辑LoginAction.java类,使其继承自ActionSupport类,并且继承一个validate方法,用于验证用户名和密码输入是否为空或者是否正确,LoginAction.java类代码最终如下:
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
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;
}
public String execute() throws Exception
{
if("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim()))
{
return "success";
}
else
{
this.addFieldError("username", "username or password error");
return "failer";
}
}
@Override
public void validate()
{
if(null == this.getUsername() || "".equals(this.getUsername().trim()))
{
this.addFieldError("username", "username.required");
}
if(null == this.getPassword() || "".equals(this.getPassword().trim()))
{
this.addFieldError("password", "password.required");
}
}
}
三、编辑配置struts.xml,对于出现不同错误指向不同的位置:
<?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>
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/result.jsp</result>
<result name="failer">login2.jsp</result>
</action>
<action name="pointConverter" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>
</package>
</struts>
图片演示操作的效果:
都为空时:
[img]http://dl.iteye.com/upload/attachment/382617/c343f2bb-d33e-3497-9291-e35a2a150a9e.jpg[/img]
用户名为空时:
[img]http://dl.iteye.com/upload/attachment/382619/2a5e84d4-b35c-33fb-bc30-08b82f106075.jpg[/img]
密码为空时:
[img]http://dl.iteye.com/upload/attachment/382621/86973a77-a687-3348-ac48-1845937e800e.jpg[/img]
都正确时:
[img]http://dl.iteye.com/upload/attachment/382623/8ec2b7bc-417d-355e-8c7b-135e2f02a8a4.jpg[/img]
推荐阅读
-
struts2学习笔记九(第9讲.Struts2的校验框架 续)
-
struts2学习笔记四(第4讲.Struts2的类型转换续)
-
struts2学习笔记八(第8讲.Struts2的校验框架)
-
struts2学习笔记三(第3讲.Struts2的类型转换)
-
struts2学习笔记十六(第16讲.Struts2的国际化)
-
struts2学习笔记六(第6讲.Struts2的输入校验续一)
-
struts2学习笔记二(第2讲.Struts2入门与配置续)
-
struts2学习笔记七(第7讲.Struts2的输入校验续二)
-
struts2学习笔记一(第1讲.Struts2入门与配置)
-
struts2学习笔记五(第5讲.Struts2的输入校验)