我在action中给request赋了两个值,不知道为何第二个
request.put("departments", departmentService.getAll());无法在jsp中的<s:select list=""> 获取....
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";
}
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中一片不正常:
但是同样用table又能获取值:
同一个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="departmentname" label="Department">
</s:select>
<s:submit></s:submit>
</s:form>
<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>
显示效果如下:
问题,为什么在获取的是这些@的地址,而不是下面输出的内容一样呢?
难道不是这样获取?
<s:select list="#request.departments" listkey="id" listvalue="departmentname" label="Department">
</s:select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
我在action中给request赋了两个值,不知道为何第二个
request.put("departments", departmentService.getAll());无法在jsp中的<s:select list=""> 获取....
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";
}
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中一片不正常:
但是同样用table又能获取值:
同一个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="departmentname" label="Department">
</s:select>
<s:submit></s:submit>
</s:form>
<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>
显示效果如下:
问题,为什么在获取的是这些@的地址,而不是下面输出的内容一样呢?
难道不是这样获取?
<s:select list="#request.departments" listkey="id" listvalue="departmentname" label="Department">
</s:select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在action中应该有一个获取下拉值的public方法
在页面select标签定义如下