Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 text 标签
Struts2 text 标签
作者:--    发布时间:2019-11-20 10:13:33

text 标签是一个通用的标记,用来渲染一个i18n的文本消息。按照三个步骤:

  • 该消息必须在一个资源包,它与作为动作具有相同的名称。在实践中,这意味着,应该为类具有相同名称的java类在同一个包创建属性文件,.properties扩展。

  • 如果具名的消息没有被发现,然后主体的标记将被用作默认的消息。

  • 如果没有正文被使用,那么该消息的名称将被使用。

让我们来检查下面的例子来理解文本标签的用法:

创建动作类:

package com.h3.struts2;

public class helloworldaction{
   private string name;

   public string execute() throws exception {
      return "success";
   }
   
   public string getname() {
      return name;
   }

   public void setname(string name) {
      this.name = name;
   }
}

创建视图

让我们包含以下内容的helloworld.jsp:

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

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" 
"http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<title>text tag example</title>
</head>
<body>
	
<s:i18n name="helloworldaction">
   <s:text name="name.success"/><br>
   <s:text name="name.xyz">message doesn't exists</s:text><br>
   <s:text name="name.msg.param">
      <s:param >zara</s:param>
   </s:text>
</s:i18n>

</body>
</html>

配置文件

让我们创建一个属性名称相同的文件,动作类包名。因此,在这种情况下,我们将创建helloworldaction.properties文件,并保持在类路径:

name.success = this is success message
name.msg.param = the param example - param : {0}

你的struts.xml中应该像这样:

<?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" />
   <constant name="struts.custom.i18n.resources" 
             value="applicationresources"/>
   
   <package name="helloaction" extends="struts-default">
      <action name="hello" 
            class="com.h3.struts2.helloworldaction" 
            method="execute">
         <result name="success">/helloworld.jsp</result>
      </action>
   </package>
</struts>

web.xml中,应该像这样:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="webapp_id" version="3.0">
   
   <display-name>struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.filterdispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

右键点击项目名称,并单击export > war file创建一个war文件。然后部署此war在tomcat的webapps目录下。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/hello.action。这会给出以下画面: 

struts text tag

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册