7、请求参数接收

  1. 对与请求参数,可以在所请求的action中添加相应的属性,写出get和set方法,在表单中配置name属性与action中属性的名称一致,提交到所在action即可;如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public class HelloWorldAction {

        private String name;
        private int id;

        public String getName() {
            return name;
        }
     
        public void setName(String name) {

            this.name = name;
        }
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
    }

    则index.jsp中表单中应如下(对应属性名一致):

    1
    2
    3
    4
    5
    <form action="${pageContext.request.contextPath }/control/department/list_execute.action" method="post">

                
    <input type="text" name="id"><br>
                <input type="text" name="name"><br>
                <input type="submit" value="提交">
    </form>
    获取值:
    1
    2
    ${id }</br>
    ${name }

  2. 对于复杂请求参数(以对象封装,实际应用如此;Person类必须使用默认构造器,不能自己添加构造器):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class HelloWorldAction {

            private Person person;
     
            public Person getPerson() {
            
    return person;
        }
     
        public void setPerson(Person person) {
           
     this.person = person;
        }
    }

    则表单如下:

    1
    2
    3
    4
    5
    <form action="${pageContext.request.contextPath }/control/department/list_execute.action" method="post">


                <input type="text" name="person.id"><br>
               
     <input type="text" name="person.name"><br>
             
       <input type="submit" value="提交">
            </form>

    获取的值的方法如下:

    1
    2
    ${person.id }</br>
    ${person.name }
  3. struts2.1.6版本存在中文请求参数乱码,可以自己添加过滤器;




原文地址:https://www.cnblogs.com/zmpandzmp/p/3649027.html