The writing of classes in 4.2 struts2
The action in Struts2 is a normal class, and its public method name is a specific action, which is equivalent to the dispatchaction in the original struts1, but it is more flexible.
For example: I have a button called addstudent, if you have a publicstring addstudent () method in the class of your action, then your action name for this button is called/addstudent.action (Magic Bar).
We correspond to "<action name=" Login "class=" org.sky.ssh.login.action.LoginAction ">" to see our login.action bar, the front of this < Actionname= "" > The thing behind it is that our web path is equivalent to "/login.action".
Org.sky.ssh.login.action.LoginAction class
Package org.sky.ssh.login.action;
Import Java.util.Map;
Import Javax.annotation.Resource;
Import Javax.servlet.RequestDispatcher;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.apache.struts2.interceptor.ServletResponseAware;
Import Org.apache.struts2.interceptor.SessionAware;
Import Org.sky.ssh.service.LoginService;
Import org.sky.ssh.util.Constants;
Import Org.sky.ssh.util.session.UserSessionInfo;
Import Com.opensymphony.xwork2.ActionSupport;
public class Loginaction extends Actionsupport implements Sessionaware, Servletrequestaware, Servletresponseaware {
Protected final Log logger = Logfactory.getlog (GetClass ());
Private Map att;
Private HttpServletRequest request = null;
Private HttpServletResponse response;
@Resource
Loginservice Loginservice;
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
public void Setservletresponse (HttpServletResponse response) {
This.response = response;
}
public void setsession (Map att) {
This.att = att;
}
Public String Execute () throws Exception {
String loginID = "";
String loginpwd = "";
HttpSession session = Request.getsession ();
String logincode = "100";
try {
loginID = (String) request.getparameter ("loginID");
Loginpwd = (String) request.getparameter ("Loginpwd");
if (Loginservice.login (loginID, loginpwd)) {
Usersessioninfo uinfo = new Usersessioninfo ();
Uinfo.setloginid (loginID);
Session.setattribute (Constants.user_session, uinfo);
} else {
Logincode = "101";
}
Request.setattribute ("Logincode", Logincode);
if (!logincode.equals ("100")) {
RequestDispatcher dispatcher = Request.getrequestdispatcher ("/jsp/login/login.jsp");
Dispatcher.forward (request, response);
}
return SUCCESS;
catch (Exception e) {
Logger.error ("Userlogin Exception:" + e.getmessage (), E);
throw new Exception ("Userlogin Exception:" + e.getmessage (), E);
}
}
}