The path to the architect 22nd Day Universal Framework Spring (iv) using STRUTS2

Source: Internet
Author: User
Tags getmessage resource ssh

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);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.