Multi-line Text in Spring String Bean as HTML template with context variables


GitHub: https://github.com/SylkeWay/synthful

There is a String bean view1Html in the context configuration file /durian/src/main/resources/springweb/pages-context.xml .

Similar to the previous page, the bean
  • is a java.lang.String bean
  • specifying constructor argument:
    <constructor-arg><value> html text </value></constructor-arg>
  • making use of the CDATA enclosure
The Java class injected with the String bean view1Html is com.synthful.angst.mvc.durian.DurianControllerAsView
@Controller
@RequestMapping("/durianview")
public class DurianControllerAsView extends AAngsta {

    private String plantFruitName;
    
    @Value(value = "${plant.fruit.name}")
    private void setPlantFruitName(String plantFruitName) {
        this.plantFruitName = plantFruitName;
        logger.info("plantFruitName 1:{}", plantFruitName);
    }
    
    @Value(value = "${plant.fruit.type}")
    private String plantFruitType;

    
    @Inject private String view1Html;
    
    @RequestMapping(value = "/v1", method = RequestMethod.GET)
    @ResponseBody
    public String requestHandler (ModelMap model) throws Exception {

        logger.info("plantFruitName 2:{}", plantFruitName);
        logger.info("plantFruitType:{}", plantFruitType);
        return view1Html;
    }
}

Note that the controller is generating its own view

  • due to the annotation @ResponseBody.
  • IOW, the String bean is the view
  • and the application-properties / context-variables are the model.


Where the context variable substitution is due to the property file appbase/properties/b.properties.
plant.fruit.name=Durian
plant.fruit.type = peanut
plant.fruit.etymology.language = Malay
plant.fruit.etymology.meaning = thorny
plant.genus = Durio
plant.tribe = Durioneae
plant.subfamily = Malvaceae
plant.order = Malvades


HTML template as multi-line text inSpring String Bean

The String bean view1Html in ScriptTemplates-context.xml
  <bean id="view1Html" class="java.lang.String" scope="prototype">
    <constructor-arg><value><![CDATA[
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html template="true"><head>
  <meta content="text/html; charset=UTF-8" http-equiv="content-type">
  <title>Durian</title>
</head><body>
<table style="text-align: left;" border="1" cellpadding="5" cellspacing="2">
  <tbody style="vertical-align: top;">
    <tr>
      <td>plant.fruit.name<br/><br/></td>
      <td>${plant.fruit.name}</td>
    </tr>
    <tr>
      <td>plant.fruit.type</td>
      <td>${plant.fruit.type}</td>
    </tr>
    <tr>
      <td>plant.fruit.etymology.language</td>
      <td>${plant.fruit.etymology.language}</td>
    </tr>
    <tr>
      <td>plant.fruit.etymology.meaning</td>
      <td>${plant.fruit.etymology.meaning}</td>
    </tr>
    <tr>
      <td>plant.genus</td>
      <td>${plant.genus}</td>
    </tr>
    <tr>
      <td>plant.tribe</td>
      <td>${plant.tribe}</td>
    </tr>
    <tr>
      <td>plant.subfamily</td>
      <td>${plant.subfamily}</td>
    </tr>
    <tr>
      <td>plant.order</td>
      <td>${plant.order}</td>
    </tr>
  </tbody>
</table>
</body></html>
    ]]></value></constructor-arg>
  </bean>

The page is accessed as http://localhost:8080/durian/v/durianview/v1 with the resulting view:
plant.fruit.name
Durian
plant.fruit.typepeanut
plant.fruit.etymology.languageMalay
plant.fruit.etymology.meaningthorny
plant.genusDurio
plant.tribeDurioneae
plant.subfamilyMalvaceae
plant.orderMalvades

No comments:

Post a Comment