假设您有一个在线表单,您必须在其中注册投票:
<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>
可以在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 -在jsf 20. - html
希望这有助于!!!!
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");