代码如下
User user=new User();
user.setAge(24);
Integer count=new Integer(1);
String str=new String("I am a boy");
Map<String, Object> map=new HashMap<>();
map.put("count",count);
map.put("str", str);
count=(Integer) map.get("count");
count=new Integer(count+1);
str=(String) map.get("str");
str="我是一个男孩!";
map.put("user",user);
user=(User) map.get("user");
user.setAge(44);
System.out.println(map.get("count")+"\t"+map.get("str")+"\t"+user);
输出结果为:
1 I am a boy User [username=null, password=null, age=44]
user的类
package com.yd.action;
import java.io.Serializable;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
public class User implements Serializable,RequestAware,SessionAware,ApplicationAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private Integer age;
private Map<String, Object> request,session,application=null;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + "]";
}
/*
* 处理登陆请求的方法
* */
public String loginControl(){
String result="fail";
if (this.getUsername()==null||this.getPassword()==null||this.getAge()==null) {
return result;
}
if("yd".equals(this.getUsername())&&"a".equals(this.getPassword())){
//在这个里面代表是登陆成功
session.put("user", this);
//如果application中有了count 那么就直接加一
if(application.get("count")==null){
application.put("count", 1);
}else{
Integer count=(Integer) application.get("count");
count=count+1;
application.put("count",count );
}
result="success";
}
return result;
}
/*
* 处理退出登陆的请求
* */
public String loginOut(){
Integer count=(Integer) application.get("count");
count=count-1;
/*
* ?
* */
application.put("count", count);
SessionMap<String, Object> sessionMap=(SessionMap<String, Object>) session;
//使得session失效
sessionMap.invalidate();
return "success";
}
@Override
public void setApplication(Map<String, Object> application) {
this.application=application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session=session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request=request;
}
}
为什么普通类型包装类就需要重新put进入才能改变map里面的值?