We all know that struts have not implemented AOP by default, and WebWork2 wants to be short, it is good, it is good, some people (Lars Hoss and Don Brown) aware of this, writing SAIF (Struts Action Invocation Framework) Make up this defect, below It is the introduction of SAIF.
SAIF installation is simple: first copy the saif-0.1.jar file to the web-inf / lib directory, then modify the struts-config.xml file, add SAIF to the plugin mode to the struts-config.xml file, As follows:
Finally, write the interceptor-config.xml file, perform the declaration of the relevant interceptor so you will complete the installation and setting of SAIF. Below we will explain the use of SAIF and its advantages in one example.
We now have this demand. In OA, the developers can modify their own notices, and others cannot modify and can only view it. We have written two Action: EditNoticeAction and ViewNoticeAction, and we pass the encoding of the parameters of these two Actions: NOTICEID. The code of the notification object is as follows (Hibernate PO mode): public class noticepo {private integer ID; // Encoded private string title; // Title prizate string content; // content private integer authorid; // Publisher Coding Private Date PublishedDate; // Release date
// getter and setter methods ..........
At the same time we have two JSP files: viewnotice.jsp and editNotice.jsp, so that we build NOTICEPO according to NOTICEID in Action, then pass it to the JSP page, JSP is responsible for displaying the data, this process is complete. But we have forgotten this, the settings of the permissions are in realization. So we need to intercept the editNoticeAction, that is, after the EditNoticeAction operation is completed, compare the AuthorId in the session in the session, if the same, the representation of the notification, pointing to the EditNotice.jsp page, otherwise, Point to the ViewNotice.jsp page. New EditNoticeInterceptor, achieve the following:. Public class EditNoticeInterceptor extends DefaultInterceptor {public ActionForward afterAction (Action action, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {Integer userId = (Integer) request.getSession () getAttribute ( "User_ID"); NOTICEPO NOTICE = (NOTICEPO) Request.getaTribute ("notice.getauthorid () == userid) {return mapping.findforward (" view ");} Return null;} The code writing of the interceptor is completed, the interceptor is declared in Interceptor-Config.xml. XML Version = "1.0" encoding = "gb2312"?> " Interceptor name = "type =" NET. Jetmaven.interceptor.editNoticeActionInterceptor "/>
In this way, we have completed our established needs and complete the permissions and business logic through intervening interceptors.
Finally, SAIF, SAIF default intercept method does not return value, and here we use ActionForward as return value, the purpose is to change the original process, return value is empty indicate continued original flow, return value does not modify the empty standard Process, this feels more likely to understand, and is more appropriate to actual situation. SAIF interceptors can be used, which is to have multiple interceptors for actions. This is also very useful. We also return to the value to determine whether to terminate this chain. If the return value is empty, the logo continues the chain, Otherwise, the chain is terminated, returning immediately. According to the SAIF structure, we wrote a DTD (right click), so that you write XML more convenient in the environment like Intellij Idea, just need "http://struts.sourceforge.net/saif.dtd" It is possible to associate with the actual DTD file. We have related changes to SAIF, in fact, the settings of the ActionForward, making it more suitable for actual conditions, welcome to discuss and discuss.
Reference:
Http://struts.sourceforge.net/saif/ http://struts.apache.org
Ezerg Programming