如何在JSF页面中通过Servlet输入HTML代码

时间:2016-07-07 19:27:59

标签: jsf servlets facelets html-injections

我想使用Java Servelet创建一个菜单,我试试这个:

dash_menu.java

package br.com.pw5k.nav;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class dash_menu extends HttpServlet {

        @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
            //response.setContentType("text/html;charset=UTF-8"); 

            String outHtml =  "<form> Dashboard Menu <br> </form>";
            outHtml = URLDecoder.decode( outHtml, "text/html" ); // and finally : Hélène
            //request.setCharacterEncoding("utf-8");
            request.setAttribute("html_insert", outHtml);
            RequestDispatcher rd = request.getRequestDispatcher("/faces/dashboard.xhtml");
            rd.forward(request, response);
    }
//        @Override
//        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 
//        PrintWriter imprimir = response.getWriter();
// 
//        imprimir.println("Método POST foi disparado!");
//    }
}

dashboard.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <ui:composition template="./logged_template.xhtml">
        <ui:define name="body">
            <h:form>
                Dashboard!
                <br/>
                <h:commandButton value="Perfil" action="user_control.xhtml"/>
            </h:form>
            #{html_insert}
        </ui:define>
    </ui:composition>
</html>

当我试着打电话时:&#34; dash_menu&#34;:

http://localhost:8084/PW5K_App/faces/dash_menu

  

but output change html tags "< >" by "&lt; &gt;"

显示输出

<form> Dashboard Menu <br> </form> 

HTML检查代码

&lt;form&gt; Dashboard Menu &lt;br&gt; &lt;/form&gt;

现在怎么解决这个问题?

0 个答案:

没有答案