作者:castellaniygw_623 | 来源:互联网 | 2023-10-11 12:40
1.目录结构
2.在form.jsp中导入City类
<%@ page import="包名.类名" %>
<%@ page import="struts2.City" %>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
为什么需要在form.jsp中导入City类?
如不导入City类,那么List cities=new ArrayList();则不能实例化。
Struts中的用法
City.java代码:
package struts2;
public class City {
private Integer cityId;
private String cityName;
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
//有参构造器
public City(Integer cityId, String cityName) {
super();
this.cityId = cityId;
this.cityName = cityName;
}
//无参构造器
public City() {
}
}
form.jsp代码:
<%@ page language="java" cOntentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="struts2.City" %>
<%
List cities=new ArrayList();
cities.add(new City(1,"AA"));
cities.add(new City(2,"BB"));
cities.add(new City(3,"CC"));
cities.add(new City(4,"DD"));
request.setAttribute("city1",cities);
%>