Struts2 injection in the Interceptor (whether to implement custom interfaces based on the action)

Source: Internet
Author: User

For example, if you often need to obtain the user currently logged on in the action, you need to obtain the session and then the user currently logged on from the session, because these steps are repeated, you can find a way to implement it in the Interceptor. You can customize an interface. As long as your action implements this interface, inject it in the Custom interceptor. That is, get the session from the interceptor and set it for injection.


Simple Example:

A custom interface, which is injected in the interceptor as long as the action implements this interface

Package COM. atguigu. surveypark. struts2; import COM. atguigu. surveypark. model. user;/*** user interest */public interface useraware {public void setuser (User user );}


A user-defined Interceptor (logon interceptor): obtains the action instance in the Interceptor. If the above interface is implemented, injection is performed. That is, the method to call the interface.
Package COM. atguigu. surveypark. struts2.interceptor; import COM. atguigu. surveypark. model. user; import COM. atguigu. surveypark. struts2.useraware; import COM. atguigu. surveypark. struts2.action. baseaction; import COM. atguigu. surveypark. struts2.action. loginaction; import COM. atguigu. surveypark. struts2.action. regaction; import COM. opensymphony. xwork2.actioninvocation; import COM. opensymphony. xwork2.interceptor. interceptor;/*** login interceptor */public class logininterceptor implements interceptor {Private Static final long serialversionuid = 423021183907541_60l; Public void destroy () {} public void Init () {}@ suppresswarnings ("rawtypes") Public String intercept (actioninvocation arg0) throws exception {baseaction action Action = (baseaction) arg0.getaction (); If (Action instanceof loginaction | action instanceof regaction) {return arg0.invoke ();} else {user = (User) arg0.getinvocationcontext (). getsession (). get ("user"); If (user = NULL) {// log on to return "login";} else {// open if (Action instanceof useraware) {// inject user to Action (useraware) action ). setuser (User) ;}return arg0.invoke ();}}}}

Action: an action that implements an Interface

Package COM. atguigu. surveypark. struts2.action; import Java. util. list; import javax. annotation. resource; import Org. springframework. context. annotation. scope; import Org. springframework. stereotype. controller; import COM. atguigu. surveypark. model. survey; import COM. atguigu. surveypark. model. user; import COM. atguigu. surveypark. service. surveyservice; import COM. atguigu. surveypark. struts2.useraware;/*** surveyaction */@ controller @ scope ("prototype") public class surveyaction extends baseaction <survey> implements useraware {Private Static final long serialversionuid = response; // inject surveyservice @ resourceprivate surveyservice; // survey the set Private list <survey> mysurveys; // accept the user object private user; public list <survey> getmysurveys () {return mysurveys;} public void setmysurveys (list <survey> mysurveys) {This. mysurveys = mysurveys;}/*** query my survey list */Public String mysurveys () {This. mysurveys = surveyservice. findmysurveys (User); Return "mysurveylistpage";}/*** new survey */Public String newsurvey () {This. model = surveyservice. newsurvey (User); Return "designsurveypage";} // inject the user object public void setuser (User user) {This. user = user ;}}

Struts. xml: Interceptor Registration

<? XML version = "1.0"?> <! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.3 // en" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Topic --> <constant name = "struts. UI. Theme" value = "simple"/> <! -- Development Mode --> <constant name = "struts. devmode "value =" true "/> <package name =" surveyparkpkg "extends =" struts-Default "namespace ="/"> <interceptors> <! -- Register the login interceptor --> <interceptor name = "logininterceptor" class = "com. atguigu. surveypark. struts2.interceptor. logininterceptor"/> <! -- Define the interceptor Stack --> <Interceptor-stack name = "surveyparkstack"> <Interceptor-ref name = "logininterceptor"/> <Interceptor-ref name = "defaultstack"/> </ interceptor-stack> </interceptors> <! -- Define the default stack --> <default-interceptor-ref name = "surveyparkstack"/> <! -- Define global results --> <global-Results> <result name = "login">/index. jsp </result> </Global-Results> <! -- Regaction --> <action name = "regaction _ *" class = "regaction" method = "{1}"> <result name = "regpage">/Reg. JSP </result> <result name = "input">/Reg. JSP </result> <result name = "success">/index. JSP </result> </Action> <! -- Loginaction --> <action name = "loginaction _ *" class = "loginaction" method = "{1}"> <result name = "loginpage">/index. JSP </result> <result name = "input">/index. JSP </result> <result name = "success">/index. JSP </result> </Action> <! -- Surveyaction --> <action name = "surveyaction _ *" class = "surveyaction" method = "{1}"> <result name = "mysurveylistpage">/mysurveylist. JSP </result> <result name = "designsurveypage">/designsurvey. JSP </result> </Action> </package> </struts>



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.