Follow these steps:
1. Create a project
2. Import jar files related to struts2
3. Configure the following in Web. xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
4. Create a struts. xml file with the following details:
When configuring the view type, you can also directly use type = "freemarker" to access the specified template. Here, I use dynamic access to the generated HTML page.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="wf" extends="struts-default"><action name="userList" class="org.Struts2"><!-- <result name="success" type="freemarker">/templates/template.ftl</result> --><result type="redirect">${url}</result></action></package></struts>
5. Create JavaBean user. Java
package org;public class User {private String name;private int id;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
6. Create creathtml. Java and call the template class to generate the specified page during execution.
Package util; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. outputstreamwriter; import Java. io. writer; import Java. util. locale; import Java. util. map; import Org. apache. struts2.servletactioncontext; import freemarker. template. configuration; import freemarker. template. template; import freemarker. template. templateexception; public class creathtml {public void Init (string FTL, string htmlname, map) throws ioexception, templateexception {// create configuration object configuration CFG = new configuration (); // set the position of the freemarker template file cfg. setservletcontextfortemplateloading (servletactioncontext. getservletcontext (), "templates"); cfg. setencoding (locale. getdefault (), "UTF-8"); // create the template object template = cfg. gettemplate (FTL); template. setencoding ("UTF-8"); // generate a static page string Path = servletactioncontext. getservletcontext (). getrealpath ("/"); file filename = new Java. io. file (path + htmlname); writer = new bufferedwriter (New outputstreamwriter (New fileoutputstream (filename), "UTF-8"); template. process (MAP, writer); writer. flush (); writer. close ();}}
7. Create the action file struts2.java as follows:
Package org; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import util. creathtml; import COM. opensymphony. xwork2.actionsupport; public class struts2 extends actionsupport {private string URL; private list <user> list; Public String execute () throws exception {creathtml = new creathtml (); list = new arraylist <user> (); User user = NULL; For (INT I = 0; I <10; I ++) {user = new user (); User. setid (I); User. setname ("name" + I); User. setage (I + I); list. add (User) ;}map <string, list <user> map = new hashmap <string, list <user> (); map. put ("userlist", list); string filename = "user.html"; string FTL = "temalate. FTL "; creathtml. init (FTL, filename, MAP); // URL as struts. the result view in XML returns the page this. url = filename; return success;} public list <user> getlist () {return list;} public void setlist (list <user> List) {This. list = List;} Public String geturl () {return URL;} public void seturl (string URL) {This. url = URL;} Private Static final long serialversionuid = 1l ;}
8. Create a template file in the root directory Templates folder template. FTL
<Table Style = "text-align: center; font-size: 11pt; width: 600px; font-family:; border-collapse: collapse "bordercolor = # 3399ff cellspacing = 0 cellpadding = 0 align = center border = 1> <tr> <TD> <B> NO. </B> </TD> <TD> <B> User Name </B> </TD> <B> age </B> </TD> </tr> <# list userlist as user> <tr> <TD >$ {user. id} </TD> <TD >$ {user. name} </TD> <TD >$ {user. age} </TD> </tr> </# list> </table>
9. Enter http: // localhost/struts2/user.html in the browser and access the action page as follows:
10. source file instance download http://download.csdn.net/detail/liweifengwf/5470833