在上一个文章的基础上,在HelloWorldAction.java的加入了name,成员变量并声称set/get方法
package action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
System.out.println("调用了Action的默认方法");
return SUCCESS;
}
}
修改.jsp文件,引入变量
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>This is ${name } first page by struts2.</h1>
</body>
</html>
运行结果: