Subclass ActionServlet to behave as central controller: Solution
In reply to message: http://www.mail-archive.com/struts-user@jakarta.apache.org/msg12014.html I am trying to proxy to my action class via /do/* rather than *.do Therefore, I need to make some modifications to my DefaultAction.java - but I'm having a little difficulty. String forwardPath = requestURI.substring( contextPath.length());forwardPath = forwardPath.substring( 0,forwardPath.indexOf("do")) + "jsp"; So I'd imagine the following should work - right? String requestURI = "/NASApp/timetracker/do/timesheetList"; forwardPath = forwardPath.substring(forwardPath.substring(forwardPath.indexOf("do/")+3, forwardPath.length());
Re: Subclass ActionServlet to behave as central controller
On Wed, 18 Jul 2001, Oleg V Alexeev wrote: > Hello Peter, > > Wednesday, July 18, 2001, 5:53:01 PM, you wrote: > > PS> Hi ppl, > > PS> Would it be possible to subclass the ActionServlet to add behaviour > PS> needed for it to behave as central controller so it could handle all the > PS> requests coming to the web application, not only the action execution > PS> requests ? In the subclassed ActionServlet I want to be able to add some > PS> logic which will determine where the request should be forwarded in case > PS> where isn't any action to be processed by ActionServlet. For example a > PS> request for /example/some_page.do could be forwarded to /some_page.jsp > PS> OR /WEB-INF/jsp/some_page.jsp. I don't want to add mappings in > PS> struts-config.xml for each page I have in the application. > > Yes, of course. You can extend ActionServlet and append your own logic > to support additional behaviors. > > -- > Best regards, > Olegmailto:[EMAIL PROTECTED] > > >
Subclass ActionServlet to behave as central controller: Solution
Hi ppl, I have done some research on this ( looking into the struts sources ) and found an interesting solution. This solution permits using ActionServlet as a single entry point to the web application. Step 1. Add a default action which will handle uknown path requests. The DefaultAction.perform method is the following: public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String requestURI = request.getRequestURI(); String contextPath = request.getContextPath(); String forwardPath = requestURI.substring( contextPath.length() ); forwardPath = forwardPath.substring( 0, forwardPath.indexOf("do")) + "jsp"; System.out.println("Requested URI = " + requestURI); System.out.println("Forwarding to: " + forwardPath); return new ActionForward(forwardPath); } The code above will forward requests to the path like /some_page.do to the page /some_page.jsp . Now you can request all the jsp pages using the .do extension. Eventually DefaultAction can be modified to forward request to jsp pages under /WEB-INF directory so you can prevent direct requests to the jsp page. Now step 2: 2. Subclass ActionServlet and add your own logic like authentication to be done before calling super.perform() method. Other interesting things can be done here: insert page nocache headers, check paths which should/shouldn't be accessed through ssl (and redirect)., add a template mechanism like sitemesh ( I don't like template tags very much ). I know this things can be done using filter API. But tomcat4 is still very buggy so I can't use it in production. Hope you find this usefull, Peter.
Re: Subclass ActionServlet to behave as central controller
Hello Peter, Wednesday, July 18, 2001, 5:53:01 PM, you wrote: PS> Hi ppl, PS> Would it be possible to subclass the ActionServlet to add behaviour PS> needed for it to behave as central controller so it could handle all the PS> requests coming to the web application, not only the action execution PS> requests ? In the subclassed ActionServlet I want to be able to add some PS> logic which will determine where the request should be forwarded in case PS> where isn't any action to be processed by ActionServlet. For example a PS> request for /example/some_page.do could be forwarded to /some_page.jsp PS> OR /WEB-INF/jsp/some_page.jsp. I don't want to add mappings in PS> struts-config.xml for each page I have in the application. Yes, of course. You can extend ActionServlet and append your own logic to support additional behaviors. -- Best regards, Olegmailto:[EMAIL PROTECTED]
Subclass ActionServlet to behave as central controller
Hi ppl, Would it be possible to subclass the ActionServlet to add behaviour needed for it to behave as central controller so it could handle all the requests coming to the web application, not only the action execution requests ? In the subclassed ActionServlet I want to be able to add some logic which will determine where the request should be forwarded in case where isn't any action to be processed by ActionServlet. For example a request for /example/some_page.do could be forwarded to /some_page.jsp OR /WEB-INF/jsp/some_page.jsp. I don't want to add mappings in struts-config.xml for each page I have in the application. Peter.