在web.xml中设置日期然后有一个HTML文件显示日期?

时间:2014-05-25 22:01:24

标签: date jsf web.xml

假设您有一个在线表格,您需要注册才能投票:

<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>

2 个答案:

答案 0 :(得分:2)

你可以借助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

希望这有助于!!!!

答案 1 :(得分:-1)

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");