从值栈获取对象

来源:互联网 发布:爱喝啤酒软件 编辑:程序博客网 时间:2024/05/21 10:41

-------------------siwuxie095

  

  

  

  

  

  

  

  

从值栈获取对象

  

  

1、具体步骤

  

1)在 Action 中向值栈放对象

  

2)在 JSP 页面中从值栈获取对象

  

  

  

  

2、具体实现

  

1)编写实体类

  

User.java:

  

package com.siwuxie095.entity;

  

  

// User实体类

public class User {

  

private String username;

private String password;

private String address;

 

public String getUsername() {

return username;

}

publicvoid setUsername(String username) {

this.username = username;

}

 

public String getPassword() {

return password;

}

publicvoid setPassword(String password) {

this.password = password;

}

 

public String getAddress() {

return address;

}

publicvoid setAddress(String address) {

this.address = address;

}

 

@Override

public String toString() {

return"User [username=" + username +", password=" + password

+", address=" + address + "]";

}

 

}

  

  

  

2)编写Action

  

ObjectAction.java:

  

package com.siwuxie095.action;

  

import com.opensymphony.xwork2.ActionSupport;

import com.siwuxie095.entity.User;

  

public class ObjectActionextends ActionSupport {

  

/*

* (1) Action中定义实体类对象

*

*因为总归是要 new的,所以就在这

*里创建,而不是声明了

*/

private User user=new User();

 

/*

* (2)提供实体类对象的 get方法即可

*/

public User getUser() {

return user;

}

 

@Override

public String execute()throws Exception {

 

/*

*如果上面仅仅是声明了实体类对象,

*那么就要在这里创建,即 new出来

*/

 

/*

* (3)在执行的方法中,向实体类对象中设置值

*/

 

user.setUsername("siwuxie095");

user.setPassword("8888");

user.setAddress("China");

 

return SUCCESS;

}

}

  

  

  

3)配置Action

  

struts.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

  

<struts>

 

<packagename="demo"extends="struts-default"namespace="/">

 

<actionname="object"class="com.siwuxie095.action.ObjectAction">

<resultname="success">/object.jsp</result>

</action>

 

</package>

  

</struts>

  

  

  

4)编写页面

  

object.jsp:

  

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

 

<!--引入 Struts2标签库 -->

<%@ taglib uri="/struts-tags" prefix="s"%>

  

<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Object</title>

</head>

<body>

  

<!--获取值栈中实体类对象的值 -->

<s:propertyvalue="user.username"></s:property>

<s:propertyvalue="user.password"></s:property>

<s:propertyvalue="user.address"></s:property>

 

</body>

</html>

  

  

  

5)访问路径

  

http://localhost:8080/工程名/object.action

  

  

  

  

  

  

  

  

【made by siwuxie095】

原创粉丝点击