struts2 adtion 中获得请求参数

两种方式:

 

1.在action 中定义与请求参数同名的属性,并提供相应的getter,setter method

 

CompanyAction.java

----------------------------------------

package org.taink.struts.action;

public class CompanyAction {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String doAdd() {
        return "success";
    }

    public String execute() {
        return "success";
    }
}

 

 

index.jsp

--------------------------------

  <body>
    <form action="control/company/list_doAdd" method="post">
       id:<input type="text" name="id"><br/>

       name:<input type="text" name="name"><br/>
        <input type="submit" value="提交">
    </form>
  </body>

 

 

message.jsp

-----------------------------------------

 

  <body>
        departmentId:${id}<br/>
        departmentName: ${name}<br/>
  </body>

 

 

 

 

2.采用复合类型接收请求参数

 

CompanyAction.java

----------------------------------------

 

package org.taink.struts.action;

import org.taink.entity.Department;
import org.taink.entity.Employee;

// struts2 采用无入侵设计(即不依赖其它框架,类,jar的支持)
public class CompanyAction {

    private Department department;
    private Employee employee;

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public String doAdd() {
        return "success";
    }

    public String execute() {
        return "success";
    }

}

 

 

index.jsp

--------------------------------

  <body>
    <form action="control/company/list_doAdd" method="post">
        departmentId:<input type="text" name="department.deptId"><br/>
        departmentName: <input type="text" name="department.deptName"><br/>
        employeeId: <input type="text" name="employee.empId"><br/>
        employeeName: <input type="text" name="employee.empName"><br/>
        <input type="submit" value="提交">
    </form>
  </body>

 

 

message.jsp

-----------------------------------------

 

  <body>
        departmentId:${department.deptId}<br/>
        departmentName: ${department.deptName}<br/>
        employeeId: ${employee.empId}<br/>
        employeeName: ${employee.empName}<br/>
  </body>

 

 

-----------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------

 

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>

    <!-- 该属性设置struts2是否支持动态方法调用,该值默认为:true,如果要关闭动态方法调用,则可以设置为:false -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />

    <package name="taink" namespace="/control/company" extends="struts-default">
        <action name="list_*" method="{1}"
            class="org.taink.struts.action.CompanyAction">
            <result name="success">/WEB-INF/page/message.jsp</result>
        </action>
    </package>
</struts>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值