首先贴一下搭配的环境:
配置:
eclipse4.3.2
jdk1.7_45
mysql 5.0+
然后切入正题:
1、login.jsp
主要是使用ognl 标签 也可使用html form表单,调用loginaction.action,以post 方式传输。
在loginaaction 经过判断,然后会有提示信息,需要用到 来显示。
登录请登录
--%>
2、struts.xml
配置
命名空间为“/”,继承”struts-default“
登录成功,则跳转到index.jsp
登录失败,则返回login.jsp
<?xml version="1.0" encoding="utf-8"?>
/p>
"-//apache software foundation//dtd struts configuration 2.3//en"
"http://struts.apache.org/dtds/struts-2.3.dtd">
/index.jsp
/login.jsp
3、loginaction.java
loginaction
继承 actionsupport 方法
重写 execute() 和 validate() 方法:
execute 方法调用从后台调用的数据库调用的值
validate 方法用于判断 用户名 和 密码输入是否为空,并提示
注意:this.addactionerror();方法的在login.jsp中调用 即可调用,即可将设置的信息默认可以直接调用,不用设置,除非在strtus.xml 中设置 。
package com.tikitoo.action;
import com.opensymphony.xwork2.actionsupport;
import com.tikitoo.service.userinfoservice;
import com.tikitoo.service.userinfoserviceimpl;
/**
* @author tikitoo1
* @see com.opensymphony.xwork2.actionsupport
* @see com.opensymphony.xwork2.actionsupport
*
*/
public class loginaction extends actionsupport {
private static final long serialversionuid = -4760561602154545441l;
/**
* struts2 默认调用方法
* @return struts2 result 返回值
*/
@override
public string execute() throws exception {
userinfoservice userinfoservice = new userinfoserviceimpl();
boolean flag = userinfoservice.loginbyusernameanduserpwd( username, userpwd);
string msg = "";
if ( flag == true) {
this.addfielderror( "true", "登录成功");
msg = "success";
} else {
this.addfieldnerror( "用户名或密码不正确!");
msg = "input";
}
return msg;
}// execute() end
/**
* 登录验证
* 重写 actionsupport 方法
*/
@override
public void validate() {
// 判断 用户名 是否为空
if ( getusername() == null || "".equals( getusername().trim() ) ) {
this.addfielderror( "username", "用户名不能为空");
}
// 判断密码是否为空
if ( getuserpwd() == null || "".equals( getuserpwd().trim() )) {
this.addfielderror("userpwd", "密码不能为空");
}
}// validate() end
private string tip;
public string gettip() {
return tip;
}
private string username;
private string userpwd;
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getuserpwd() {
return userpwd;
}
public void setuserpwd(string userpwd) {
this.userpwd = userpwd;
}
}
用户名密码输入不正确:
用户名输入正确,则登录成功:
以上就是本文的全部内容,希望对大家的学习有所帮助。
希望与广大网友互动??
点此进行留言吧!