struts2标签和JavaBean
我在action中通过数据库查询语句查询出一个JavaBean实例,然后用request传给jsp页面,怎么才能用标签库把各个属性之获取出来呢?我在jsp页面中通过<%%>方法已经能成功获取各个属性值了。我想知道怎么用标签库莱获取属性值,
JavaBean:
public class Problem {
private String id;
private String title;
private String content;
private Date date;
private String author;
private String direction;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
}
实例化JavaBean:
public String loadWithId(){
Problem problemOne = new Problem();
String id = (String) request.getParameter("id");
problemOne = ps.loadWithId(id);
System.out.println(problemOne.getTitle());
request.setAttribute("problemOne", problemOne);
return "loadOne";
}
然后通过struts2的配置文件跳转到jsp
我用<%%>方法获取了各个属性值
<% Problem problemOne = (Problem) request.getAttribute("problemOne");
%>
<%=problemOne.getTitle() %>
能够正常输出结果
用<s:bean>进行输出————没有属性值输出
<s:bean name="org.bianqiao.model.Problem"var="pro">
<s:property value="#pro.title"/>
</s:bean>
这是怎么回事啊?怎么才能用标签获取属性值呢?
------最佳解决方案--------------------el表达式${beanName.xxxx}
<s:property value="beanName.xxxx"/>
如果后台会用域对象的话就这样
------其他解决方案--------------------
<s:property value="id"/>
------其他解决方案--------------------用s标签或者c标签,c标签是java就有的,s标签是struts的,在jsp页面开头加上
<%@ taglib prefix="s" uri="/struts-tags"%>
或者
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
------其他解决方案--------------------将这些属性封装到类 Problem 里面,然后放到session对象里 在用jstl标签就可以用了
------其他解决方案--------------------用s标签或者c标签,c标签是java就有的,s标签是struts的,在jsp页面开头加上
<%@ taglib prefix="s" uri="/struts-tags"%>