无法显示属性的值,显示的是对象的地址" />

为什么我在无法显示属性的值,显示的是对象的地址

发布于 2022-09-02 15:23:41 字数 4384 浏览 9 评论 0

DepartmentDao.java

package com.service;

import com.entities.Department;

import java.util.List;


public class DepartmentDao extends BaseDao {


    public List<Department> getAll() {

        String hql = "from Department";

        return getSession().createQuery(hql).list();
    }

}

DepartmentService.java

package com.service;

import com.entities.Department;

import java.util.List;


public class DepartmentService {

    private DepartmentDao departmentDao;

    public void setDepartmentDao(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    public List<Department> getAll() {

        return departmentDao.getAll();
    }
}

action.java

package com.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.service.DepartmentService;
import com.service.EmployeeService;
import org.apache.struts2.interceptor.RequestAware;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;


public class EmployeeAction extends ActionSupport implements RequestAware {

    private EmployeeService employeeService;
    private DepartmentService departmentService;
    private InputStream inputStream;
    private Map<String, Object> request;
    private Integer id;

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }


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

    public String list() {

        request.put("employee", employeeService.getAll());

        return "list";
    }


//这里赋值给request了,怎么获取不到实际的内容呢?
    public String input() {

        request.put("departments", departmentService.getAll());

        return "input";
    }


    public String delete() {

        try {
            employeeService.delete(id);
            inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            try {
                inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
        }

        return "delete";
    }

    @Override
    public void setRequest(Map<String, Object> map) {


        this.request = map;
    }


}

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
    <title>Title</title>
</head>
<body>

<h4>Employee Input info</h4>

<s:debug></s:debug>
<s:form action="emp-save" method="post">


    <s:textfield name="lastname" label="lastName"></s:textfield>
    <s:textfield name="email" label="Email"></s:textfield>
    <s:textfield name="birth" label="Birth"></s:textfield>
    <s:textfield name="createtime" label="Createtime"></s:textfield>

    <%--这里为什么获取的是地址呢?--%>
    <s:select list="#request.departments" listkey="id" listvalue="department.departmentname" label="Department" name="department.id">
    </s:select>
    <s:submit></s:submit>

</s:form>


<%--这里对比的,这里能够获取value="#request.departments--%>
<table border="1" cellpadding="2" cellspacing="0">

    <tr>
        <td>ID</td>
        <td>DEPARTMENT</td>

    </tr>
    <s:iterator value="#request.departments">

        <tr>
            <td>${id}</td>
            <td>${departmentname}</td>
        </tr>


    </s:iterator>
</table>

</body>
</html>

显示:为什么显示的是Deapartment的对象地址呢?这说明listKey和listValue不起作用啊..怎么回事?

图片描述

图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2022-09-09 15:23:41

难道不需要遍历吗?你拿到数据到jsp后是一个list,应该需要<c:foreach> 来进行遍历出每个对象再挨个点属性取值吧?

眼趣 2022-09-09 15:23:41

我也遇到同样的问题,兄台怎么解决的?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文