Struts2之Action三种接收参数形式与简单的表单验证

  有了前几篇的基础,相信大家对于Struts2已经有了一个很不错的认识,本篇我将为大家介绍一些关于Action接收参数的三种形式,以及简单的表单验证实现,下面进入正题,首先我们一起先来了解一下最基本的Action接收参数的形式,直接在我们的Action类中添加成员变量,这样就可以完成Action接收参数的操作,具体内容详见代码展示:

  这里我们以登录验证为例,进行代码展示,首先是我们的Action类:

/***
 * 最基本的接收参数形式
 * @author jho
 * http://localhost:8080/Struts3/index!login?name="123"&password="456"
 */
@SuppressWarnings("serial")
public class User extends ActionSupport {
    
    private String name;
    private int pwd;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPwd() {
        return pwd;
    }

    public void setPwd(int pwd) {
        this.pwd = pwd;
    }
    
    public String login(){
        System.out.println("name="+name+"  Pwd="+pwd);
        if(123456==pwd){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
    
}

  下面我们一起来看一下Action的第二种接收参数的形式,通过User类对象来完成整个的参数接收,首先我们创建一个User类:

public class User {
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

  然后通过User对象在Action中进行用户提交参数的接收:

/***
 * Dormain Model
 * @author jho
 * http://localhost:8080/Struts4/index!login?user.name="123"&user.password="456"
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
    
    private User user;
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String login(){
        System.out.println("name="+user.getName()+"  Pwd="+user.getPassword());
        return SUCCESS;
    }
    
}

  最后是通过Struts的ModelDriven<User>来进行用户参数的接收,第一步创建我的User类对象,第二部配置我们的Action:

/***
 * Model Driven
 * @author jho
 * http://localhost:8080/Struts5/index!login?name="123"&password="456"
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements ModelDriven<User>{
    
    private User user = new User();
    
    public String login(){
        System.out.println("name="+user.getName()+"  Pwd="+user.getPassword());
        return SUCCESS;
    }

    public User getModel() {
        return user;
    }
    
}

  所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象。


  对于简单的表单验证知识,就为大家介绍一下addFieldError()方法:

@SuppressWarnings("serial")
public class InputErrorFilter extends ActionSupport{

    private String name;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    public String login(){
        System.out.println(name+"");
        if(name!=null && name.equals("hpu")){
            return SUCCESS;
        }else{
            this.addFieldError("nameError", "用户名输入有误");
            this.addFieldError("nameError", "ul li形式输出错误信息");
            return ERROR;
        }
    }
    
}

  我们的错误处理页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
            操作失败 <br>
            输入参数:<s:text name="name" /><br/>
        <s:fielderror fieldName="nameError" theme="simple"/><br/>
        <s:property value="errors.nameError[0]"/><br/>
        <s:debug></s:debug>
  </body>
</html>

  本篇为大家介绍的知识点比较简单,没有特别复杂,难于理解的知识点,相信大家简单熟悉就可以掌握。后续内容持续更新中...

posted @ 2017-03-07 20:48  小破孩123  阅读(686)  评论(6编辑  收藏  举报