课程表

Struts2 教程

Struts2 标签

Struts2 集成

工具箱
速查手册

Struts2 date标签

当前位置:免费教程 » Java相关 » Struts2
data标签允许以快速简单的方式格式化日期。用户可以指定自定义日期格式(例如“dd/MM/yyyy hh:mm”),可以生成易读的符号(例如“在2小时14分钟内”),或者可以使用属性文件中的key:“struts.date.format”来回退到预定义的格式。

创建action类

  1. package com.w3xue.struts2;
  2. import java.util.*;
  3. public class HelloWorldAction{
  4. private Date currentDate;
  5. public String execute() throws Exception{
  6. setCurrentDate(new Date());
  7. return "success";
  8. }
  9. public void setCurrentDate(Date date){
  10. this.currentDate = date;
  11. }
  12. public Date getCurrentDate(){
  13. return currentDate;
  14. }
  15. }

创建视图

让我们创建包含以下内容的HelloWorld.jsp
  1. <%@ page contentType="text/html; charset=UTF-8" %>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <html>
  4. <head>
  5. <title>Hello World</title>
  6. </head>
  7. <body>
  8. <h2>Current Date</h2>
  9. <h3>Day/Month/Year Format</h3>
  10. <s:date name="currentDate" format="dd/MM/yyyy" />
  11. <h3>Month/Day/Year Format</h3>
  12. <s:date name="currentDate" format="MM/dd/yyyy" />
  13. </body>
  14. </html>

配置文件

你的struts.xml应该如下所示:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="true" />
  7. <package name="helloworld" extends="struts-default">
  8. <action name="hello"
  9. class="com.w3xue.struts2.HelloWorldAction"
  10. method="execute">
  11. <result name="success">/HelloWorld.jsp</result>
  12. </action>
  13. </package>
  14. </struts>
你的web.xml应该如下所示:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. id="WebApp_ID" version="3.0">
  8. <display-name>Struts 2</display-name>
  9. <welcome-file-list>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12. <filter>
  13. <filter-name>struts2</filter-name>
  14. <filter-class>
  15. org.apache.struts2.dispatcher.FilterDispatcher
  16. </filter-class>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>struts2</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. </web-app>
右键单击项目名称,然后单击“Export”> “WAR File”以创建WAR文件。然后在Tomcat的webapps目录中部署WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/hello.action,将显示以下界面:
系统信息
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号