本文主要是介绍【ssh学习笔记】struts2的action与Spring,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//由struts-spring-plugin管理,不需要加@Resource,或在<span style="font-family: Arial; font-size: 14px; line-height: 26px;">applicationContext.xml</span>中也不需要配置该bean
//需要注入的对象也不需要加@Resource
public class JsonAction extends ActionSupport {/*** */private static final long serialVersionUID = -9040282050464969108L;private String isSuccess = "success";private List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();private UserManager userManager;@JSON(name="result")public String getIsSuccess() {return isSuccess;}public void setIsSuccess(String isSuccess) {this.isSuccess = isSuccess;}public List<Map<String, Object>> getUsers() {return users;}public void setUsers(List<Map<String, Object>> users) {this.users = users;}@JSON(serialize=false)public UserManager getUserManager() {return userManager;}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//@Resourcepublic void setUserManager(UserManager userManager) {this.userManager = userManager;}@Overridepublic String execute() throws Exception {List<User> userList = userManager.listUsers();for(User user : userList){Map<String, Object> jsonMap = new HashMap<String, Object>();jsonMap.put("id", user.getId());jsonMap.put("username", user.getUsername());jsonMap.put("password", user.getPassword());users.add(jsonMap);}return SUCCESS;}}
在struts.xml中,action的class属性指向具体的class时,action是由struts-spring-plugin来管理,即不需要在action中写上@Resource,@Component等annotaion
相反,如果将action完全交由spring来管理,则class属性设置为action的bean id,
<package name="json" extends="json-default" namespace="/"><action name="jsonAction" class="<span style="font-family: Arial, Helvetica, sans-serif;">jsonAction</span><span style="font-family: Arial, Helvetica, sans-serif;">" method="execute"></span>
<result type="json" /></action></package>
所以需要在action中添加annotation,或者在:applicationContext.xml添加bean
//完全由spring管理,需要加@Resource,或在<span style="font-family: Arial; font-size: 14px; line-height: 26px;">applicationContext.xml</span>中配置该bean
//需要注入的对象需要加@Resource
@Component
public class JsonAction extends ActionSupport {/*** */private static final long serialVersionUID = -9040282050464969108L;private String isSuccess = "success";private List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
private UserManager userManager;@JSON(name="result")public String getIsSuccess() {return isSuccess;}public void setIsSuccess(String isSuccess) {this.isSuccess = isSuccess;}public List<Map<String, Object>> getUsers() {return users;}public void setUsers(List<Map<String, Object>> users) {this.users = users;}@JSON(serialize=false)
@Resource
public UserManager getUserManager() {return userManager;}
<span> </span>
<span> </span>//@Resourcepublic void setUserManager(UserManager userManager) {this.userManager = userManager;}@Overridepublic String execute() throws Exception {List<User> userList = userManager.listUsers();for(User user : userList){Map<String, Object> jsonMap = new HashMap<String, Object>();jsonMap.put("id", user.getId());jsonMap.put("username", user.getUsername());jsonMap.put("password", user.getPassword());users.add(jsonMap);}return SUCCESS;}}
这篇关于【ssh学习笔记】struts2的action与Spring的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!