action動態訪問調用,struts的action訪問servlet的IOC方式與非IOC方式

 2023-11-19 阅读 38 评论 0

摘要:這是IOC方式,要實現相應接口 package loginAction; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; public cla
這是IOC方式,要實現相應接口
package loginAction;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;


public class IoCAction extends ActionSupport implements ServletRequestAware{ ? ?
? ? private String userName;
? ? private String password;
? ? private HttpServletRequest request;
? ? public String getUserName() {
? ? ? ? return userName;
? ? }


? ? /**
? ? ?* @param userName the userName to set
? ? ?*/
? ? public void setUserName(String userName) {
? ? ? ? this.userName = userName;
? ? }


? ? public String getPassword() {
? ? ? ? return password;
? ? }

? ? public void setPassword(String password) {
? ? ? ? this.password = password;
? ? }


? ? //必須實現該方法,該方法是接口中的方法
? ? public void setServletRequest(HttpServletRequest hsr) {
? ? ? ?request=hsr;
? ? }
? ? public String execute() throws Exception{
? ? ? ? if(getUserName().equals("QQ")&&getPassword().equals("123")){
? ? ? ? ? ? ?//通過request對象獲取session對象
? ? ? ? ? ? ?HttpSession session=request.getSession();
? ? ? ? ? ? ?//把登錄名傳入session中
? ? ? ? ? ? ?session.setAttribute("userName", this.getUserName());
? ? ? ? ? ? ?return SUCCESS;
}
else{
? ? ? ? ? ? return INPUT;
}
? ? }

}


action動態訪問調用?下面來看非IOC方式的代碼



package loginAction;


import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;


public class NoIoCAction extends ActionSupport{
? ? private String userName;
? ? private String password;


? ? /**
? ? ?* @return the userName
? ? ?*/
? ? public String getUserName() {
? ? ? ? return userName;
? ? }


? ? /**
? ? ?* @param userName the userName to set
? ? ?*/
? ? public void setUserName(String userName) {
? ? ? ? this.userName = userName;
? ? }


? ? /**
? ? ?* @return the password
? ? ?*/
? ? public String getPassword() {
? ? ? ? return password;
? ? }


? ? /**
? ? ?* @param password the password to set
? ? ?*/
? ? public void setPassword(String password) {
? ? ? ? this.password = password;
? ? }
? ? public String execute() throws Exception{
? ? ? ? if(getUserName().equals("QQ")&&getPassword().equals("123")){
? ? ? ? ? ? /*調用ServletActionContext的getRequest()方法獲取HttpServletRequest類的對象request對象。*/
? ? ? ? ? ? HttpServletRequest request=ServletActionContext.getRequest();
? ? ? ? ? ? //調用request對象的getSession()方法獲取session對象
? ? ? ? ? ? HttpSession session=request.getSession();
? ? ? ? ? ? //調用session對象的方法設置數據
? ? ? ? ? ? session.setAttribute("userName", this.userName);
? ? ? ? ? ? session.setAttribute("password", this.password);
? ? ? ? ? ? return SUCCESS;
}
else{
? ? ? ? ? ? return INPUT;
}
? ? }?
}


版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183448.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息