java - 为什么我在<s:select list="">无法显示属性的值,显示的是对象的地址
天蓬老师
天蓬老师 2017-04-17 17:52:33
[Java讨论组]

DepartmentDao.java

package com.service;

import com.entities.Department;

import java.util.List;


public class DepartmentDao extends BaseDao {


    public List 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 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 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 map) {


        this.request = map;
    }


}

jsp:

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



    Title



Employee Input info

<%--这里为什么获取的是地址呢?--%> <%--这里对比的,这里能够获取value="#request.departments--%>
ID DEPARTMENT
${id} ${departmentname}

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
迷茫

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

ringa_lee

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号