Building an App with the frostillic.us Framework, Part 7
Tue Oct 07 21:00:41 EDT 2014
- Building an App with the frostillic.us Framework, Part 1
- Building an App with the frostillic.us Framework, Part 2
- Building an App with the frostillic.us Framework, Part 3
- Building an App with the frostillic.us Framework, Part 4
- Building an App with the frostillic.us Framework, Part 5
- Building an App with the frostillic.us Framework, Part 6
- Building an App with the frostillic.us Framework, Part 7
Well, it's been much longer than planned, and this topic isn't actually particularly groundbreaking, but the series returns!
- Define the data model
- Create the view and add it to an XPage
- Create the editing page
- Add validation and translation to the model
- Add notification to the model
- Add sorting to the view
- Basic servlet
- REST with Angular.js
One of the edge features of the Framework is that it assists in writing DesignerFacesServlet
servlets - which are sort of like XAgents but written directly as Java classes, without an XPage component.
Before I explain how they work in the Framework, there's a caveat: these servlets do not have (reliable) sessionAsSigner
access. The reason for this is that IBM's mechanism for determining the signer doesn't cover the case of just having a Java class. That said, it does have access to the rest of the XPages environment, including the same instances of managed beans available to XPages.
With that unpleasantness aside, here's an example servlet:
package servlet; import javax.faces.context.FacesContext; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import frostillicus.xsp.servlet.AbstractXSPServlet; public class ExampleServlet extends AbstractXSPServlet { @Override protected void doService(HttpServletRequest req, HttpServletResponse res, FacesContext facesContext, ServletOutputStream out) throws Exception { out.println("hello"); } }
Once you create that class (in the package "servlet"), it is available as "/foo.nsf/xsp/exampleServlet". As with an XPage, you can add arbitrary stuff after the servlet name with a "/" and in the query string. Unlike in an XPage, the servlet name is not removed from the path info. So, for example, this method:
protected void doService(HttpServletRequest req, HttpServletResponse res, FacesContext facesContext, ServletOutputStream out) throws Exception { Map<String, String> param = (Map<String, String>) ExtLibUtil.resolveVariable(facesContext, "param"); out.println("param: " + param); out.println("pathInfo: " + req.getPathInfo()); }
...with this URL fragment:
foo.nsf/xsp/exampleServlet/foo?bar=baz...results in this in the browser:
param: {bar=baz} pathInfo: /xsp/exampleServlet/foo
By default, the result is served as text/plain
, but you can change that as usual, with res.setContentType(...)
.
For most apps, a servlet like this isn't necessary. And for apps that do have a use for servlets, the XAgent and ExtLib-control routes may be more useful. Nonetheless, I've found a number of uses for these, and I appreciate that I don't have a bunch of extra non-UI XPages cluttering up the list.