text
?標簽是用于呈現(xiàn) I18n 文本信息的通用標簽。按照以下三個步驟之一:讓我們查看下面的例子來理解 text 標簽的用法:
package cn.w3cschool.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; } }
<%@ 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>
name.success = This is success message name.msg.param = The param example - param : {0}你的 struts.xml 應(yīng)該如下所示:
<?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="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>你的 web.xml 應(yīng)該如下所示:
<?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>現(xiàn)在,右鍵單擊項目名稱,然后單擊“Export”> “WAR File”以創(chuàng)建 WAR 文件。然后在 Tomcat 的 webapps 目錄中部署 WAR 文件。最后,啟動 Tomcat 服務(wù)器并嘗試訪問 URL http://localhost:8080/HelloWorldStruts2/hello.action,將顯示以下界面:
更多建議: