简体   繁体   English

重定向到自定义servlet时如何从JSF添加HttpServletRequest属性?

[英]How to add HttpServletRequest attribute from JSF when redirecting to a custom servlet ?

I am implementing sort of ImageServlet serving an Image, inspired by BalusC example here http://balusc.omnifaces.org/2007/04/imageservlet.html 我正在实施一种ImageServlet,该图像服务于Image,受BalusC示例的启发,此处为http://balusc.omnifaces.org/2007/04/imageservlet.html

My servlet implementation expects to have a parameter "Animal" of type Animal 我的servlet实现期望有一个Animal类型的参数“ Animal”

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LOGGER.info("On ImageServlet");

        // Get Animal from request.
        Animal animal = (Animal) request.getAttribute("Animal");

My JSF page have at some point a graphicImage tag 我的JSF页面在某些时候具有graphicImage标记

<h:graphicImage value="faces/image?Animal=#{animalForm.animal}" />

My animalForm bean is serving the Animal correctly 我的animalForm豆正确地服务了动物

public Animal getAnimal()
{
    LOGGER.info("Current animal is : " + this.animal);
    return this.animal;
}

I know that the Animal=#{animalForm.animal} is wrong as it adds a Parameter, which will be of type String and not an Attribute but I can not find the way to add a (Typed) attribute here. 我知道Animal=#{animalForm.animal}是错误的,因为它添加了一个参数,该参数的类型为String而不是Attribute,但是我在这里找不到添加(Typed)属性的方法。

I have also read about more up-to-date way of doing the same thing with omnifaces, but right now I am on an learning curve and I have (unfortunatelly) started by project with ManagedBean and Tomcat so I would need to switch to CDI objects first. 我还阅读了有关使用多功能工具进行相同操作的最新方法,但是现在我处在学习曲线上,并且(不幸地)我从ManagedBean和Tomcat的项目开始,所以我需要切换到CDI对象第一。 I plan to do that latter once I have something working at the first place 我计划一开始有工作时就做后者

There is a way to pass an(imal) object as part of query string but you will need to 有一种方法可以将(对象)对象作为查询字符串的一部分传递,但是您需要

  • convert your animal to JSON 将动物转换为JSON
  • and then to encode 'JSON animal' to string conforming URL Query string standard. 然后将“ JSON动物”编码为符合字符串的URL 查询字符串标准。

Bean 豆角,扁豆

Convert animal to JSON string and encode to URL string: 将动物转换为JSON字符串并编码为URL字符串:

import com.google.gson.Gson;
import java.net.URLEncoder;
//.....

Animal animal;

public Animal getAnimal() {
    return animal;
}

//...

String jsonAnimalUrlEncoded;

public String getJsonAnimalUrlEncoded() throws UnsupportedEncodingException{
    Gson gson=new Gson();
    String jsonAnimal=gson.toJson(animal);
    jsonAnimalUrlEncoded=URLEncoder.encode(jsonAnimal, "UTF-8");
    System.out.println("URL encoded JSON animal == " + jsonAnimalUrlEncoded);
    return jsonAnimalUrlEncoded;
}

JSF page (animal image) JSF页面(动物图像)

<h:graphicImage value="faces/image?animal=#{animalForm.jsonAnimalUrlEncoded}" />

This will pass URL encoded JSON animal to your servlet as animal attribute. 这会将URL编码的JSON动物作为animal属性传递给servlet。

Servlet side Servlet端

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    LOGGER.info("On ImageServlet");

    // Get json animal url encoded string from request
    String jsonAnimalUrlEncoded = request.getAttribute("animal");
    //convert it to JSON animal
    String jsonAnimal=URLDecoder.decode(getJsonAnimalUrlEncoded(),"UTF-8");
    //transform json animal to animal java object
    Animal animal = (Animal) new Gson().fromJson(jsonAnimal, Animal.class);

    System.out.println("My name is: " + animal.getName());

    //...
}

Servlet doGet method will catch jsonAnimalUrlEncoded that you passed from JSF page, decode it back to 'normal' JSON string and convert JSON string to real animal Java object. Servlet doGet方法将捕获从JSF页面传递的jsonAnimalUrlEncoded ,将其解码回“正常” JSON字符串,并将JSON字符串转换为真实的Java对象。

( it was fun writing this since Class name was Animal :) ) 写这篇文章很有趣,因为类名是Animal :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM