原始类型和包装类型
//首先创建两个实体类 user 和 address user中包含address
package cn.jbit.bean; public class User { //用户类 private String name; //姓名 private int age; //年龄 private Address address;//住址 public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package cn.jbit.bean; public class Address { // 家庭地址 private String country; // 国家 private String city; // 城市 private String street; // 街道 public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
2.创建出一个action先创建出两个成员变量 然后在核心方法中实例化成员变量。
package cn.jbit.action; import com.opensymphony.xwork2.ActionSupport; import cn.jbit.bean.*; public class ShowOriginalAndJavaBeanAction extends ActionSupport { private User user; private String message; public String execute(){ setUser(new User()); getUser().setName("jbit"); getUser().setAge(23); Address address=new Address(); address.setCountry("中国"); address.setCity("北京"); address.setStreet("长安街"); getUser().setAddress(address); setMessage("展示原始类型和JavaBean"); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
核心配置:struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- <constant name="struts.custom.i18n.resources" value="message"/> --> <package name="default" extends="struts-default"> <action name="showOriginalAndJavaBeanAction" class="cn.jbit.action.ShowOriginalAndJavaBeanAction"> <result>result.jsp</result> </action> </package> </struts>
index.jsp
<body> 信息:<s:property value="message" default="展示数据" /><br/> 姓名:<s:property value="user.name" /><br/> 年龄:<s:property value="user.age" /><br/> 国家:<s:property value="user.address.country" /> </body>
在搜索栏输入localhost:8080/Struts2_OGNL/showOriginalAndJavaBeanAction访问
时间: 2024-10-23 02:41:56