Exemple de balise bean Struts 2

Téléchargez-le - lien://wp-content/uploads/2010/07/Struts2-Bean-Tag-Example.zip[Struts2-Bean-Tag-Example.zip]

La balise « bean » de Struts 2 est utilisée pour instancier une classe dans la page JSP. Dans ce tutoriel, vous utiliserez la balise « bean » pour instancier une classe nommée « HelloBean », définissez sa propriété via l’élément « param » et imprimez la valeur.

1. haricot simple

Une classe simple, utilisez ensuite la balise bean pour l’instancier.

package com.mkyong.common.action;

public class HelloBean{

    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

2. Action

Une classe d’action pour transférer la demande.

  • BeanTagAction.java **

package com.mkyong.common.action;

import com.opensymphony.xwork2.ActionSupport;

public class BeanTagAction extends ActionSupport{

    public String execute() {
        return SUCCESS;
    }

}

2. Exemple de balise de haricot

Une page JSP pour montrer l’utilisation de la balise " bean " pour instancier le " HelloBean ".

Dans la balise « bean », vous pouvez attribuer un nom au bean via un attribut « var ». Vous pourrez ensuite accéder au bean via # var bean name ou sa valeur de propriété via # var bean name.property .

  • bean.jsp **

<%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
<head>
</head>

<body>
<h1>Struts 2 Bean tag example</h1>

<s:bean name="com.mkyong.common.action.HelloBean" var="hello">
  <s:param name="msg">Hello Bean Tag</s:param>
</s:bean>

The HelloBean's msg property value : <s:property value="#hello.msg"/>

</body>
</html>

3. struts.xml

Liez-le ~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="default" namespace="/" extends="struts-default">

    <action name="beanTagAction"
        class="com.mkyong.common.action.BeanTagAction" >
        <result name="success">pages/bean.jsp</result>
    </action>

    </package>
</struts>

4. Démo

http://localhost : 8080/Struts2Example/beanTagAction.action

  • Sortie**

Exemple de balise bean Struts 2

Référence

Documentation]

lien://tag/struts2/[struts2]