![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
struts2 actionsupport如何进行用户登录验证
展开全部
在Action中做校验时,可以直接在execute()方法中进行一些简单的校验
但Actionsupport类给我们提供了方法validate(),使我们的校验更加清晰简单
当我们重写此方法时对应的struts.xml的result也要写上对应的
<result name="input">
/input.jsp
</result>
经过测试,大致是这样的,validate方法执行校验不成功时后直接返回结果INPUT而不去执行execute方法了
当校验成功时才去执行execute方法,可以返回结果SUCCESS
但Actionsupport类给我们提供了方法validate(),使我们的校验更加清晰简单
当我们重写此方法时对应的struts.xml的result也要写上对应的
<result name="input">
/input.jsp
</result>
经过测试,大致是这样的,validate方法执行校验不成功时后直接返回结果INPUT而不去执行execute方法了
当校验成功时才去执行execute方法,可以返回结果SUCCESS
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private User user;//用户实体类
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int login(User user){//验证登陆方法
try{
//注册驱动(以mysql数据库为例)
Class.forName("com.mysql.jdbc.Driver");
//获得连接
String url="jdbc:mysql://localhost/sms";
String user="root";
String pwd="root";
Connection conn = DriverManager.getConnection(url, user, pwd);
//获得statement对象
Statement stmt=conn.createStatement();
//获得用户输入的名和密码
String name=user.getUserName();
String pwd=user.getPassword();
System.out.println("用户名是"+name+",用户密码是"+pwd);
//数据库查询语句
String sql="SELECT * FROM USER WHERE name='" + name + "' and pwd='" + pwd + "'";
ResultSet res=stmt.executeQuery(sql);
if(res.next())
return 1;
return 0;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 1;
}
}
public String execute(){
System.out.println("这里是调用UserAction中的execute()方法");
int i=login(user);
if(i==1)
return "success";
else
return "error";
}
}
public class UserAction extends ActionSupport{
private User user;//用户实体类
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int login(User user){//验证登陆方法
try{
//注册驱动(以mysql数据库为例)
Class.forName("com.mysql.jdbc.Driver");
//获得连接
String url="jdbc:mysql://localhost/sms";
String user="root";
String pwd="root";
Connection conn = DriverManager.getConnection(url, user, pwd);
//获得statement对象
Statement stmt=conn.createStatement();
//获得用户输入的名和密码
String name=user.getUserName();
String pwd=user.getPassword();
System.out.println("用户名是"+name+",用户密码是"+pwd);
//数据库查询语句
String sql="SELECT * FROM USER WHERE name='" + name + "' and pwd='" + pwd + "'";
ResultSet res=stmt.executeQuery(sql);
if(res.next())
return 1;
return 0;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 1;
}
}
public String execute(){
System.out.println("这里是调用UserAction中的execute()方法");
int i=login(user);
if(i==1)
return "success";
else
return "error";
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-26
展开全部
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private User user;//用户实体类
private static String SUCCESS = "success";
private static String ERROR = "error";
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int login(User user){//验证登陆方法
try{
//注册驱动(以mysql数据库为例)
Class.forName("com.mysql.jdbc.Driver");
//获得连接
String url="jdbc:mysql://localhost/sms";
String user="root";
String pwd="root";
Connection conn = DriverManager.getConnection(url, user, pwd);
//获得statement对象
Statement stmt=conn.createStatement();
//获得用户输入的名和密码
String name=user.getUserName();
String pwd=user.getPassword();
System.out.println("用户名是"+name+",用户密码是"+pwd);
//数据库查询语句
String sql="SELECT * FROM USER WHERE name='" + name + "' and pwd='" + pwd + "'";
ResultSet res=stmt.executeQuery(sql);
if(res.next())
return 1;
return 0;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 1;
}
}
public String execute(){
System.out.println("这里是调用UserAction中的execute()方法");
int i=login(user);
if(i==1)
return SUCCESS;
else
return ERROR;
}
}
/***用户类**/
public class User{
private String username;
private String password;
get/set ...
}
public class UserAction extends ActionSupport{
private User user;//用户实体类
private static String SUCCESS = "success";
private static String ERROR = "error";
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int login(User user){//验证登陆方法
try{
//注册驱动(以mysql数据库为例)
Class.forName("com.mysql.jdbc.Driver");
//获得连接
String url="jdbc:mysql://localhost/sms";
String user="root";
String pwd="root";
Connection conn = DriverManager.getConnection(url, user, pwd);
//获得statement对象
Statement stmt=conn.createStatement();
//获得用户输入的名和密码
String name=user.getUserName();
String pwd=user.getPassword();
System.out.println("用户名是"+name+",用户密码是"+pwd);
//数据库查询语句
String sql="SELECT * FROM USER WHERE name='" + name + "' and pwd='" + pwd + "'";
ResultSet res=stmt.executeQuery(sql);
if(res.next())
return 1;
return 0;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 1;
}
}
public String execute(){
System.out.println("这里是调用UserAction中的execute()方法");
int i=login(user);
if(i==1)
return SUCCESS;
else
return ERROR;
}
}
/***用户类**/
public class User{
private String username;
private String password;
get/set ...
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询