【问题标题】:Set a date in web.xml then have an HTML file show the date?在 web.xml 中设置日期然后让 HTML 文件显示日期?
【发布时间】:2014-05-25 22:01:24
【问题描述】:

假设您有一个在线表格,您必须在其中注册才能投票:

<html>

<head>
</head>

<body>

<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Address: <input type="text" name="address"><br>
City: <input type="text" name="city"><br>
State: <input type="text" name="state"><br>
Zip: <input type="text" name="zip"><br>
Phone Number: <input type="text" name="phone"><br>

Affiliation:<br>
<input type="radio" name="affil" value="demo">Democrat<br>
<input type="radio" name="affil" value="green">Green Party<br>
<input type="radio" name="affil" value="liber">Liberterian<br>
<input type="radio" name="affil" value="repub">Republican<br>
<input type="radio" name="affil" value="None">Unafiiliated<br>

<input type="submit" value="Submit">

</form>

</body>
</html>

我想在表单中的某个地方放置文本

“下一次选举:[日期值]”

但是我不想在 HTML 中硬编码日期,而是希望将日期插入到我的 web.xml 文件中,然后让 HTML 文件引用该日期。

但是,我对 XML 以及它如何与 HTML 连接非常缺乏经验。我如何在 XML 中创建一个日期,然后让 HTML 文件引用它?

这是我当前的“web.xml”文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <!-- Tell faces what extension it should be using to find files. -->
 <context-param>
   <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
   <param-value>.xhtml</param-value> 
 </context-param>

 <!-- Turn on debugging for faces. --> 
 <context-param>
   <param-name>javax.faces.PROJECT_STAGE</param-name>
   <param-value>Development</param-value> 
 </context-param>

 <!-- This section loads the servlet that process faces into your app. -->
 <servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>   

 <!-- This tells the server where to send requests for faces pages. -->
 <servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>  

 <welcome-file-list>
   <welcome-file>index.xhtml</welcome-file>
 </welcome-file-list>
</web-app>

【问题讨论】:

    标签: date jsf web.xml


    【解决方案1】:

    您可以借助 jsf 隐式 EL 对象来实现。

     <context-param>
        <description>Date to be shown</description>
        <param-name>showDate</param-name>
        <param-value>26/05/2014</param-value>
    </context-param>   
    

    你可以借助下面的EL表达式在UI中显示

          #{initParam['showDate']}  
    

    要了解 JSF 隐式 EL 对象,请参阅以下文章

    http://balusc.blogspot.in/2011/09/communication-in-jsf-20.html

    希望这有帮助!!!!

    【讨论】:

      【解决方案2】:

      web.xml 中的条目:- 将上下文参数定义为 web.xml 中的日期

      <context-param>
              <description>Registration Date.</description>
              <param-name>registrationDate</param-name>
              <param-value>02/02/2014</param-value>
          </context-param>
      

      JSP 更改:- 在 jsp 中读取日期如下。

      ServletContext context = pageContext.getServletContext();
      String registrationDate = context.getInitParameter("registrationDate");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-02
        • 2016-02-13
        • 2020-09-25
        • 1970-01-01
        相关资源
        最近更新 更多