SSH:几种从Action取值到JSP种的方法

一,Action-驱动模型

Action:

public Object getModel() {
		// TODO 自动生成的方法存根
		student.setId(1);
		student.setName("张三");
		return student;
	}

取值:

<s:property value="name" />
<s:property value="id" />

二,Action-JavaBean

Action:

public class OGNLAction extends ActionSupport {
	private Student student;

	public OGNLAction() {
		student = new Student();
		student.setId(1);
		student.setName("张三");
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public String execute() throws Exception {
		return SUCCESS;
	}

}

取值:

#两种方法
	<s:property value="student.id" />
	<s:property value="student['id']" />
	<s:property value="student.name" />
	<s:property value="student['name']" />

三,Action-属性内置

Action:

public class OGNLAction extends ActionSupport {
	private String name;
	private String id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

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

	public OGNLAction() {
		name = "tom";
		id = "1";
	}

	public String execute() throws Exception {
		return SUCCESS;
	}

}

取值:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<s:property value="name" />
	<s:property value="id" />
</body>
</html>

四,集合类型

Action:

ServletActionContext.getServletContext().setAttribute("booklist", book_list3);
//将数组丢到Application或Session中

取值:

<s:iterator value="#application['booklist']">或者<s:iterator value="#application.booklist">

		<s:property value="BookId" />
			
		<s:property value="BookName" />
			
		<s:property value="author" />
		
</s:iterator>

猜你喜欢

转载自blog.csdn.net/qq_42192693/article/details/88836223