Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Preliminary include capability #32

Merged
merged 7 commits into from Sep 8, 2011
@@ -1,6 +1,7 @@
class JspController < ApplicationController
def index
- servlet_request['message'] = 'Hello from Rails!'
+ servlet_request['message'] = "This is being rendered by a JSP!"
forward_to '/jsp/index.jsp'
+ render :nothing => true
end
end
@@ -0,0 +1,6 @@
+class JspForwardController < ApplicationController
+ def index
+ request.forward_to '/jsp/index.jsp', {:message => "This is being rendered by a JSP!"}
+ render :nothing => true
+ end
+end
@@ -0,0 +1,5 @@
+class JspIncludeController < ApplicationController
+ def index
+ @today = Time.now.strftime("%m/%d/%Y")
+ end
+end
@@ -0,0 +1,3 @@
+<div>This line of text came from a Rails ERB, today's date is <%= @today %></div>
+<br/>
+<div>And this is content from a JSP: <br\> <b><%= request.render "/jsp/include.jsp", {"message" => "I'm a message that came from Rails!"} %> </b></div>
@@ -0,0 +1 @@
+Yoohoo look at me! I am an included JSP. The servlet request attribute is [<%= request.getAttribute("message") %>]
@@ -41,3 +41,11 @@
store
output.string
end
+
+get '/jsp_forward' do
+ request.forward_to "/jsp/index.jsp", {"message" => "Hello from JSP"}
+end
+
+get '/jsp_include' do
+ erb :jsp_include
+end
@@ -0,0 +1 @@
+<div class="inside_jsp">Yoohoo look at me! I am an included JSP. The servlet request attribute is [<%= request.getAttribute("message") %>]</div>
@@ -0,0 +1,13 @@
+<html>
+ <head><title>Sinatra forwards to JSP</title></head>
+ <link rel="stylesheet" href="http://yui.yahooapis.com/2.8.0r4/build/reset/reset-min.css" type="text/css" media="screen, projection">
+ <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection">
+ <link rel="shortcut icon" href="favicon.ico">
+ <body>
+ <div id="content">
+ <div class="inside_jsp">
+ This page has been generated from a JSP. The servlet request attribute is <%= request.getAttribute("message") %>
+ </div>
+ </div>
+ </body>
+</html>
@@ -6,4 +6,4 @@ p {margin: 20px 0px;}
ul, ol {margin:0 1.5em 1.5em 1.5em;}
ul {list-style-type:disc;}
ol {list-style-type:decimal;}
-
+.inside_jsp {border: dotted 1px; margin-left: 20px;}
@@ -0,0 +1,13 @@
+<html>
+ <head><title>Sinatra includes JSP</title></head>
+ <link rel="stylesheet" href="http://yui.yahooapis.com/2.8.0r4/build/reset/reset-min.css" type="text/css" media="screen, projection">
+ <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection">
+ <link rel="shortcut icon" href="favicon.ico">
+ <body>
+ <div id="content">
+ <div>This line of text came from ERB.</div>
+ <br/>
+ <div>And this is content from a JSP: <br\> <%= request.render "/jsp/include.jsp", {"message" => "I'm a message that came from Sinatra!"} %></div>
+ </div>
+ </body>
+</html>
@@ -46,7 +46,7 @@ public void init(FilterConfig config) throws ServletException {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
- RackEnvironment env = new ServletRackEnvironment((HttpServletRequest) request, context);
+ RackEnvironment env = new ServletRackEnvironment((HttpServletRequest) request, (HttpServletResponse) response, context);
RackResponseEnvironment responseEnv = new ServletRackResponseEnvironment((HttpServletResponse) response);
HttpServletRequest httpRequest = maybeAppendHtmlToPath(request, env);
HttpServletResponse httpResponse = (HttpServletResponse) response;
@@ -47,7 +47,7 @@ public void init(ServletConfig config) {
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
- dispatcher.process(new ServletRackEnvironment(request, rackContext), new ServletRackResponseEnvironment(response));
+ dispatcher.process(new ServletRackEnvironment(request, response, rackContext), new ServletRackResponseEnvironment(response));
}
@Override
@@ -11,6 +11,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
+import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
@@ -43,7 +44,7 @@ public int doEndTag() throws JspException {
@Override public String getQueryString() { return params; }
@Override public String getServletPath() { return ""; }
};
- RackResponse result = app.call(new ServletRackEnvironment(request, context));
+ RackResponse result = app.call(new ServletRackEnvironment(request, (HttpServletResponse) pageContext.getResponse(), context));
pageContext.getOut().write(result.getBody());
} finally {
factory.finishedWithApplication(app);
@@ -12,6 +12,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
+import javax.servlet.http.HttpServletResponse;
+
import java.io.IOException;
import java.io.InputStream;
@@ -22,14 +24,17 @@
@SuppressWarnings("deprecation")
public class ServletRackEnvironment extends HttpServletRequestWrapper
implements HttpServletRequest, RackEnvironment {
+
private String scriptName;
private String requestURI;
private String requestURIWithoutQuery;
private String pathInfo;
private RackContext rackContext;
+ private HttpServletResponse servletResponse;
- public ServletRackEnvironment(HttpServletRequest request, RackContext rackContext) {
+ public ServletRackEnvironment(HttpServletRequest request, HttpServletResponse response, RackContext rackContext) {
super(request);
+ this.servletResponse = response;
this.rackContext = rackContext;
}
@@ -40,6 +45,14 @@ public RackContext getContext() {
public InputStream getInput() throws IOException {
return getInputStream();
}
+
+ /**
+ * The underlying HttpServletResponse
+ * @return
+ */
+ public HttpServletResponse getResponse() {
+ return servletResponse;
+ }
/**
* Define the script name as the context path + the servlet path.
Oops, something went wrong.