JSP:使用jsp包含'out'(jspWriter)的委托来改变表达式的行为 [英] JSP: Using a delegate for 'out' ( jspWriter) with jsp includes to change the behaviour of Expressions
问题描述
说,有一个类 MyJSPWriter
,它扩展了 JspWriter
并实现了所有抽象方法..并且 print(String)
被修改为添加一些特殊行为,因此所有字符串表达式都将被区别对待(可能我可以将它用于某些特殊编码或类似的东西) - 这是一个简化的例子):
package com.myproject.base;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
公共类MyJSPWriter扩展了JspWriter {
JspWriter out = null;
public MyJSPWriter(JspWriter out){
super(0,true);
this.out = out;
}
@Override
public String toString(){
return out.toString();
}
@Override
public void clear()抛出IOException {
out.clear();
}
@Override
public void clearBuffer()抛出IOException {
out.clearBuffer();
}
@Override
public void close()抛出IOException {
out.close();
}
@Override
public void flush()抛出IOException {
out.flush();
}
@Override
public int getRemaining(){
return out.getRemaining();
}
@Override
public void newLine()抛出IOException {
out.newLine();
}
@Override
public void print(boolean b)throws IOException {
out.print(b);
}
@Override
public void print(char c)throws IOException {
out.print(c);
}
@Override
public void print(int i)throws IOException {
out.print(i);
}
@Override
public void print(long l)throws IOException {
out.print(l);
}
@Override
public void print(float f)抛出IOException {
out.print(f);
}
@Override
public void print(double d)抛出IOException {
out.print(d);
}
@Override
public void print(char [] s)抛出IOException {
out.print(s);
}
@Override
public void print(String s)throws IOException {
out.print(Processed String:+ s);
}
@Override
public void print(Object obj)throws IOException {
out.print(obj);
}
@Override
public void println()抛出IOException {
out.println();
}
@Override
public void println(boolean x)throws IOException {
out.println(x);
}
@Override
public void println(char x)throws IOException {
out.println(x);
}
@Override
public void println(int x)throws IOException {
out.println(x);
}
@Override
public void println(long x)抛出IOException {
out.println(x);
}
@Override
public void println(float x)抛出IOException {
out.println(x);
}
@Override
public void println(double x)throws IOException {
out.println(x);
}
@Override
public void println(char [] x)抛出IOException {
out.println(x);
}
@Override
public void println(String x)throws IOException {
out.println(x);
}
@Override
public void println(Object x)throws IOException {
out.println(x);
}
@Override
public void write(char [] cbuf,int off,int len)抛出IOException {
out.write(cbuf,off,len );
}
}
我有jsp(比如 Main.jsp
)看起来像这样:
<%@ page import =com.myproject.base%>
<%out = new MyJSPWriter(out); %GT;
<%=Hello World%>
所以,在我的输出中,它会显示为
已处理字符串:Hello World
现在,如果我有一些更多的jsp:includes,并且可能更多inlcudes中的每一个..
例如:
Main.jsp
<%@ page import =com.myproject.base%>
<%out = new MyJSPWriter(out); %GT;
<%=Hello World%>
< jsp:include page =Sub1.jsp>< / jsp:include>
< jsp:include page =Sub2.jsp>< / jsp:include>
Sub1.jsp
<%=来自sub1.jsp的Hello World%>
Sub2.jsp
<%=来自sub2.jsp的Hello World%>
< jsp:include page =Sub3.jsp>< / jsp:include>
依旧......
但是所有的子jsp都会有 out
他们自己的对象...: - (
如何我们可以在不添加的情;%>
每个这些文件中的
(因为我正在尝试使用)这在传统应用程序中)?
还有其他方法可以解决这个问题吗?
附加信息:
当我们查看生成的jsp的.java文件时,
这是代码的一部分看起来像
public void _jspService(HttpServletRequest request,HttpServletResponse response)
抛出java.io.IOException,ServletException {
PageContext pageContext = null ;
HttpSession session = null;
ServletContext application = null;
ServletCo nfig config = null;
JspWriter out = null;
对象页=这个;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType(text / html);
pageContext = _jspxFactory.getPageContext(this,request,response,
null,true,8192,true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out = new MyJSPWriter(out);
//等等写作内容....
如果查看生成的jsp类的顶部,您将看到以下行
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
现在,一种可能的解决方案来定制 out
对象是有一个自定义的 JspFactory
implmentation。
步骤
创建自定义JspFactory实现
公共类MyJspFactory扩展JspFactory {
private static JspFactory _myFactory = null;
public MyJspFactory(JspFactory factory){
_myFactory = factory;
}
//查找_myFactory并执行相同操作的所有抽象方法
public PageContext getPageContext(Servlet servlet,ServletRequest请求,ServletResponse响应,字符串errorPageURL,boolean needsSession, int bufferSize,boolean autoflush){
PageContext myCtxt = _myFactory.getPageContext(....)
//创建一个customPageContext并将myCtxt包装在其中并返回
}
}
创建CutsomPageContext类
public class MyPageContext extends PageContext {
private PageContext _ctxt = null;
public void setPageContext(PageContext ctxt){
_ctxt = ctxt;
}
//使用_ctxt对象实现所有抽象方法
@override
public JspWriter getOut(){
JspWriter _out = _ctxt 。出去();
//如前所述,使用MyJSPWriter包装_out对象并返回;
}
}
现在在初始阶段servlets,添加以下行
JspFactory newFactory = new MyJspFactory(JspFactory.getDefaultFactory());
JspFactory.setDefaultFactory(newFactory);
我还没试过。但从概念上讲它应该有效。如果您能通过此获得您想要的结果,请告诉我们。
祝你好运!
Say, there is a class MyJSPWriter
which extends JspWriter
and implemented all the abstract methods.. And print(String )
is modified to add some special behavior, so that all the expression that are strings would be treated differently ( May be I could use this for some special encoding or something like that-- this is a simplified example):
package com.myproject.base;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
public class MyJSPWriter extends JspWriter{
JspWriter out = null;
public MyJSPWriter(JspWriter out) {
super(0, true);
this.out = out;
}
@Override
public String toString() {
return out.toString();
}
@Override
public void clear() throws IOException {
out.clear();
}
@Override
public void clearBuffer() throws IOException {
out.clearBuffer();
}
@Override
public void close() throws IOException {
out.close();
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public int getRemaining() {
return out.getRemaining();
}
@Override
public void newLine() throws IOException {
out.newLine();
}
@Override
public void print(boolean b) throws IOException {
out.print(b);
}
@Override
public void print(char c) throws IOException {
out.print(c);
}
@Override
public void print(int i) throws IOException {
out.print(i);
}
@Override
public void print(long l) throws IOException {
out.print(l);
}
@Override
public void print(float f) throws IOException {
out.print(f);
}
@Override
public void print(double d) throws IOException {
out.print(d);
}
@Override
public void print(char[] s) throws IOException {
out.print(s);
}
@Override
public void print(String s) throws IOException {
out.print("Processed String: " + s);
}
@Override
public void print(Object obj) throws IOException {
out.print(obj);
}
@Override
public void println() throws IOException {
out.println();
}
@Override
public void println(boolean x) throws IOException {
out.println(x);
}
@Override
public void println(char x) throws IOException {
out.println(x);
}
@Override
public void println(int x) throws IOException {
out.println(x);
}
@Override
public void println(long x) throws IOException {
out.println(x);
}
@Override
public void println(float x) throws IOException {
out.println(x);
}
@Override
public void println(double x) throws IOException {
out.println(x);
}
@Override
public void println(char[] x) throws IOException {
out.println(x);
}
@Override
public void println(String x) throws IOException {
out.println(x);
}
@Override
public void println(Object x) throws IOException {
out.println(x);
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
out.write(cbuf, off, len);
}
}
And I have jsp ( say Main.jsp
) which looks something like this:
<%@page import="com.myproject.base"%>
<% out = new MyJSPWriter(out); %>
<%= " Hello World" %>
So, in my output, it will appear like
Processed String: Hello World
Now, if i have some more jsp:includes, and may more inlcudes in each one of them.. eg:
Main.jsp
<%@page import="com.myproject.base"%>
<% out = new MyJSPWriter(out); %>
<%= " Hello World" %>
<jsp:include page="Sub1.jsp"></jsp:include>
<jsp:include page="Sub2.jsp"></jsp:include>
Sub1.jsp
<%= " Hello World from sub1.jsp" %>
Sub2.jsp
<%= " Hello World from sub2.jsp" %>
<jsp:include page="Sub3.jsp"></jsp:include>
and so on...
But all the sub jsp's will have out
objects of their own... :-(
How can we bring the same behaviour to all included jsps without adding
<% out = new MyJSPWriter(out); %>
in each one of these files ( because i'm trying to use this in a legacy application) ?
Is there any other way i can approach this?
Additional Information: When we look at the generated .java file of the jsp, this is how part of the code look like
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out = new MyJSPWriter(out);
// and so on writing content ....
If you look at the top of generated jsp class, you will see the following line
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
Now, one possible solution to customize out
object is to have a custom JspFactory
implmentation.
Steps
Create a custom JspFactory implementation
public class MyJspFactory extends JspFactory {
private static JspFactory _myFactory = null;
public MyJspFactory(JspFactory factory) {
_myFactory = factory;
}
//All abstract methods which looks up _myFactory and does the same thing
public PageContext getPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) {
PageContext myCtxt = _myFactory.getPageContext(....)
//Create a customPageContext and wrap myCtxt in it and return
}
}
Create a CutsomPageContext class
public class MyPageContext extends PageContext {
private PageContext _ctxt = null;
public void setPageContext(PageContext ctxt) {
_ctxt = ctxt;
}
//Implement all abstract methods using _ctxt object
@override
public JspWriter getOut() {
JspWriter _out = _ctxt.getOut();
//Wrap _out object using MyJSPWriter as mentioned in question and return back;
}
}
Now during the init face of the servlets, add the following lines
JspFactory newFactory = new MyJspFactory(JspFactory.getDefaultFactory());
JspFactory.setDefaultFactory(newFactory);
I have not tried it out. But conceptually it should work. Please let us know if you could achieve what you wanted through this.
Good Luck!
这篇关于JSP:使用jsp包含'out'(jspWriter)的委托来改变表达式的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!