s:select list="#request.test" 显示对象的内存地址值,不能显示属性值?

发布于 2022-09-02 11:17:06 字数 3763 浏览 12 评论 0

被这个问题搞死了
在idea中编辑的.......

现在显示效果如下:显示不到文字的,显示一个object的内存地址,但是在下面的table就能显示出来....
图片描述

我的代码:

Test.java

package com.entities;

public class Test {
    private int id;
    private String departmentName;

    public int getId() {
        return id;
    }

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

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }


}

DepartmentDao.java

package com.service;

import com.entities.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.util.List;


public class DepartmentDao {

    private SessionFactory factory;

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    public Session getSeesion() {

        return factory.getCurrentSession();
    }

    public List<Test> getAll() {

        String hql = "from Test";
        return getSeesion().createQuery(hql).list();

    }
}

DepartmentService.java

package com.service;

import com.entities.Test;

import java.util.List;


public class DepartmentService {

    private DepartmentDao departmentDao;

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

    }

    public List<Test> getAll() {

        return departmentDao.getAll();
    }
}

DepartmentAction.java

package com.actions;

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

import java.util.Map;


public class DepartmentAction extends ActionSupport implements RequestAware {

    private Map<String, Object> request;
    private DepartmentService departmentService;

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

    }

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

        this.request = map;
    }

    public String list() {

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

        return "list";
    }
}

jsp:

这样写也一样:

<s:select list="#request.test" listkey="id" listvalue="departmentName" headerValue="输入部门">

</s:select>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>show result</title>
</head>
<body>

<h4>show department info</h4>

<s:debug></s:debug>
<s:form action="/" method="post" namespace="/">

    <s:select list="#request.test" listkey="#request.test.id" listvalue="#request.test.departmentName" headerValue="输入部门">
    </s:select>

    <s:submit></s:submit>

</s:form>



<h4>对比,检测request是否有值</h4>
<table border="1" cellpadding="2" cellspacing="0">

    <tr>
        <td>ID</td>
        <td>DEPARTMENT</td>
        <td>DEPARTMENT-Object</td>

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

        <tr>
            <td>${id}</td>
            <td>${departmentName}</td>
            <td>${test}</td>
        </tr>


    </s:iterator>
</table>


</body>
</html>

使用的是spring4,hibernate5,struts2...

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文