第9章
Web 网页模板技术
习题答案
第 9 章 Web 网页模板技术
习题答案
1.Velocity 中属性引用的赋值方式都有哪些?。
答案: Velocity 中属性引用的赋值方式有两种: ● 使用 Hashtable 对象赋值。 ● 使用方法赋值。
2.简述几种 Velocity 的指令及其用法。
答案: 下面是几种常见的 Velocity 指令: ● set 指令:给引用赋值。 ● 使 if/elseif/else 指令:类似于 Java 语言中的 if…elseif…else 指令,可进行条件控制。 ● set 指令:给引用赋值。 ● foreach 指令:进行遍历循环。 ● macro 指令:用来定义宏。
3.简述 FreeMarker 的宏指令及其用法?
答案: 在 FreeMarker 中, 宏是一个用户自定义指令, 定义完宏后就可以在模板中用@来使用宏。 宏是使用 macro 指令来定义的。例如使用下面的代码定义宏: <#macro book bookname> 书的名字:${bookname} #macro> 使用下面的代码在模板中使用宏: <#macro book bookname> 书的名字:${bookname} <@book bookname="JSP教程"/>
4.在 FreeMarker 中如何格式化数字?
答案: 在 FreeMarker 中可使用#{expre;format}这种形式格式化数字,其中的 format 使用 mN
–1–
第9章
Web 网页模板技术
习题答案
或 MN 表示,mN 代表小数部分最小 N 位,MN 代表小数部分最大 N 位。
5.使用 Velocity 模板在控制台输出一本图书的相关信息。
实例内容:在控制台输出图书的 id、图书的名称、图书的作者、图书的价格、图书的出 版社、图书的 出版日期,要求使用方法赋值的方式给各个属性赋值。 答案: (1)创建一个 Java 工程,名称为 booksys,导入工程所需的 jar 文件 velocity-dep-1.5.jar。 (2)在工程下创建模板文件 showBookInfo.vm,该模板文件可输出图书的相关信息,模 板文件的代码如下: 图书id: $book.bookid 图书名称:$book.bookname 图书作者:$book.bookauthor 图书价格:$book.bookprice 出版社: $book.bookcencern 出版日期:$book.bookdate (3)在工程的 sunyang 包下创建一个类 Book.java,该类为一个 JavaBean,代表图书的 相关信息。类 Book 的代码如下: public class Book { private int bookid; //id号 private String bookname; //书名 private String bookauthor; //作者 private double bookprice; //价格 private String bookcencern; //出版社 private String bookdate; //出版日期 public int getBookid() { return bookid; } public void setBookid(int bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getBookauthor() { return bookauthor; } public void setBookauthor(String bookauthor) {
–2–
第9章
Web 网页模板技术
习题答案
this.bookauthor = bookauthor; } public double getBookprice() { return bookprice; } public void setBookprice(double bookprice) { this.bookprice = bookprice; } public String getBookcencern() { return bookcencern; } public void setBookcencern(String bookcencern) { this.bookcencern = bookcencern; } public String getBookdate() { return bookdate; } public void setBookdate(String bookdate) { this.bookdate = bookdate; } } (4)在工程的 sunyang 包下创建一个类 BookVelocity.java,该类用来给模板提供值。类 BookVelocity 的代码如下: import java.io.StringWriter; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; public class BookVelocity{ public static void main(String[] args) { try { Velocity.init();//初始化Velocity引擎 } catch (Exception e) { e.printStackTrace(); } VelocityContext context = new VelocityContext();// 初始化Velocity上下文 Book book=new Book(); //给Book属性赋值 book.setBookid(200803); book.setBookname("JSP教程"); book.setBookauthor("sunyang");
–3–
第9章
Web 网页模板技术
习题答案
book.setBookprice(53.2); book.setBookcencern("XX出版社"); book.setBookdate("2008-02-03"); context.put("book", book);//将Book对象放入Velocity上下文中 StringWriter writer = new StringWriter(); try { //把模板和上下文结合起来 Velocity.mergeTemplate("showBookInfo.vm", "GBk", context, writer); } catch (Exception e) { e.printStackTrace(); } System.out.println(writer.toString());//控制台上输出 } } (4)运行程序,控制台上的输出结果如下图所示:
6.使用 FreeMarker 编写一个用户登录实例。
实例内容:当用户在登录页面中没有输入任何信息就登录时,提示用户需要输入用户名 和密码方可登录,若用户输入错误的用户名或密码,提示用户输入的用户名或密码错误,若 用户输入正确的用户名和密码,登录成功,页面跳转到欢迎用户登录页面。 (1)创建一个 Web 工程,名称为 login,导入工程所需的 jar 文件, (2)在 WebRoot 下新建一个文件夹 freemarker,在 freemarker 创建模板文件 login.ftl、 welcome.ftl 和 error.ftl,其中 login.ftl 为用户登录页面,welcome.ftl 为欢迎页面,error.ftl 为登 录失败页面。login.ftl 的代码如下:
新编实用英语综合教程2 unit 1 课后习题答案 P4-1 ①What are you doing tonight② i was wondering ③i wish i could ④write a term paper ⑤ some other time then ⑥ That's right P4-2 1)are you doing anything special tommorrow evening ? 2) i would like invite you to come to my birthday party . would you like to join us ? :3) Good , will you come at 7:00 ? 4) Lemonade if you must bring sth . P4-3 1) what are you going to do this weekend ? 2) Thunderstorm is on this weekend . 3) the early or the late show 4) Maybe go to KFC (kentucky Fried Chicken ) or a Coffee Shop . 5) i 'd rather go to KFC 6) when and where shall we meet ? P5-1 ①tomorrow②ball game③skiing④f or a long time ⑤very warm⑥agree P6-2
① No , she doen't ②going to the ball game and skiing ③She heard it on the radio ④ No , he doesn't ⑤ He will give claire a call P6-3 ① the time and place ②fine ③formal written ④ in writing ⑤ at the bottom ⑥ attend ⑦ in person or by phone ⑧ comfortable P7-4 1) d 2) C 3) a 4) b P8-1 1) You should not fell committed until you know what the invitation 2) it is : apology , reason for refusal , thanks for the invitation . 3) it means each pays one's own check when eating in a restaurant . 4) the word "sometime ". P9-2 1) instances 2) informal 3)entertain 4) host 5) casual 6)suggestion P9-3 ① invent an excuse later ② present problems ③ explicit④ specific time mentioned ⑤ Yes , th at would be nice . P9-4 1) instance 2) present 3) hostess 4. appropriate 5)invent 6) entertain 7) explicit 8) identify P9-5 !) For convenience , the photo will be shown in time sequence. 2) You are required to stop your car after an accident .
习题 1. 在HTML中,