struts2和freemarker集成实例

 最近要开始做项目了,项目中可能会用到Struts2和Freemarker集成,所以做了个实例给大家分享一下。

 

        前期的配置web.xml,导入jar包就不给大家介绍了,这些内容做过struts2的朋友的非常熟悉,如果有不会的大家可以找一下帮助文档或者上互联网上搜索一下解决方法。

 

        看过struts2结构图的朋友们都应该知道struts2的展现层不再是像struts1那样单纯的jsp了,它能够集成很多展现层技术,例如今天说的freemarker等等。

 

 

 

        既然上面都说到了freemarker仅仅是一个展现层技术,那么我们自然会想到,freemarker应该和struts2中配置的action标签中的result有关系,其实就是这样的,我们知道struts2的result标签就是匹配跳转路径控制展示层的作用,所以我们利用struts2和freemarker的最近本操作就是配置result标签,把原先的result标签的type变成freemarker,跳转的文件变成freemarker的模板文件即可。配置如下:

 

  1. <struts>   
  2.   
  3.  <package name="struts"extends="struts-default">   
  4.   
  5. <action name="userLogin"class="com.test.action.LoginAction" method="userLogin">   
  6.   
  7.        <result name="success" type="freemarker">   
  8.   
  9.          /WEB-INF/templates/user.ftl   
  10.   
  11.        </result>   
  12.   
  13.      </action>   
  14.   
  15.    </package>   
  16.   
  17. </struts>  


 

 

       如果我们要是用struts2的注解方式来配置action的话,那么我们就会这样配置:

 

  1. @Action(value = "showadd"results ={ @Result(name = "success"type = "freemarker"location ="/flt/adduser.ftl") })  

 

 

 

        总之就是要改result的类型和跳转文件类型即可。

 

        最后我们说一下我们怎么将模板中的变量变成实际的值,这个也是比较简单的,我们这个工作要在action中实现,具体就是我们要将模板中的变量定义到action中,并且生成get和set方法,最后在相应的业务方法中将这个变量赋值,这样struts2就会自动的为我们将值填写到模板中,形成真实的展现页面,最后跳转显示给用户。

 

       Freemarker的模板实例:

 

  1. <html>  
  2.   
  3. <head>  
  4.   
  5. <meta http-equiv="Content-Type" content="text/html;charset=gb2312">  
  6.   
  7. <title>Insert title here</title>  
  8.   
  9. </head>  
  10.   
  11. <body>  
  12.   
  13.        <formmethodformmethod="post" action="${formaction}" name="${formname}">  
  14.   
  15.                 
  16.   
  17.               <input id="${adduser_username}" name="${adduser_username}" type="${text}" value="" />  
  18.   
  19.               <input id="${adduser_password}" name="${adduser_password}" type="${text}" value=""/>  
  20.   
  21.               <input id="${adduser_submit}" name="${adduser_submit}" type="${submit}" value="adduser" />  
  22.   
  23.        </form>  
  24.   
  25. </body>  
  26.   
  27. </html>  

 

 

      对应的action:

 

  1. private Stringformaction;  
  2.   
  3.   
  4.   
  5. private Stringformname;  
  6.   
  7.   
  8.   
  9. private Stringadduser_username;  
  10.   
  11.   
  12.   
  13. private Stringadduser_password;  
  14.   
  15.   
  16.   
  17. private Stringadduser_submit;  
  18.   
  19.   
  20.   
  21. private Stringtext;  
  22.   
  23.   
  24.   
  25. private Stringsubmit;  
  26.   
  27.   
  28.   
  29. public StringgetAdduser_username() {  
  30.   
  31.        returnadduser_username;  
  32.   
  33. }  
  34.   
  35.   
  36.   
  37. publicvoidsetAdduser_username(String adduser_username) {  
  38.   
  39.        this.adduser_username = adduser_username;  
  40.   
  41. }  
  42.   
  43.   
  44.   
  45. public StringgetAdduser_password() {  
  46.   
  47.        returnadduser_password;  
  48.   
  49. }  
  50.   
  51.   
  52.   
  53. publicvoidsetAdduser_password(String adduser_password) {  
  54.   
  55.        this.adduser_password = adduser_password;  
  56.   
  57. }  
  58.   
  59.   
  60.   
  61. public StringgetAdduser_submit() {  
  62.   
  63.        returnadduser_submit;  
  64.   
  65. }  
  66.   
  67.   
  68.   
  69. publicvoid setAdduser_submit(Stringadduser_submit) {  
  70.   
  71.        this.adduser_submit = adduser_submit;  
  72.   
  73. }  
  74.   
  75.   
  76.   
  77. public StringgetText() {  
  78.   
  79.        returntext;  
  80.   
  81. }  
  82.   
  83.   
  84.   
  85. publicvoidsetText(String text) {  
  86.   
  87.        this.text =text;  
  88.   
  89. }  
  90.   
  91.   
  92.   
  93. public StringgetSubmit() {  
  94.   
  95.        returnsubmit;  
  96.   
  97. }  
  98.   
  99.   
  100.   
  101. publicvoidsetSubmit(String submit) {  
  102.   
  103.        this.submit =submit;  
  104.   
  105. }  
  106.   
  107.   
  108.   
  109. public StringgetFormaction() {  
  110.   
  111.        returnformaction;  
  112.   
  113. }  
  114.   
  115.   
  116.   
  117. publicvoidsetFormaction(String formaction) {  
  118.   
  119.        this.formaction =formaction;  
  120.   
  121. }  
  122.   
  123.   
  124.   
  125. public StringgetFormname() {  
  126.   
  127.        returnformname;  
  128.   
  129. }  
  130.   
  131.   
  132.   
  133. publicvoidsetFormname(String formname) {  
  134.   
  135.        this.formname =formname;  
  136.   
  137. }  
  138.   
  139.   
  140.   
  141.   
  142.   
  143. @Action(value= "showadd", results = { @Result(name ="success", type = "freemarker", location = "/flt/adduser.ftl") })  
  144.   
  145. public Stringshowadd() throws Exception {  
  146.   
  147.        /* 
  148.  
  149.         * FreeMarkerUtil freeMarkerUtil=newFreeMarkerUtil(); 
  150.  
  151.         * Map<String,Object> root=newHashMap<String,Object>(); //为数据模型添加值 
  152.  
  153.         * root.put("formaction","adduser"); root.put("formname", "adduser"); 
  154.  
  155.         *  
  156.  
  157.         * FileWriter out=new FileWriter(newFile("/adduser.ftl")); 
  158.  
  159.         * freeMarkerUtil.printFile("/flt","adduser.ftl",root, out); 
  160.  
  161.         */  
  162.   
  163.        setFormaction("adduser");  
  164.   
  165.        setFormname("adduser");  
  166.   
  167.        setAdduser_password("password");  
  168.   
  169.        setAdduser_submit("adduser_submit");  
  170.   
  171.        setAdduser_username("username");  
  172.   
  173.        setText("text");  
  174.   
  175.        setSubmit("submit");  
  176.   
  177.        return"success";  
  178.   
  179. }  

 

 

        这样我们运行就能够看到我们的一个添加页面,需要填入用户名和密码。

展开阅读全文

freemarkerstruts2集成

06-11

FreeMarker template error!rnrnCould not load taglib informationrnThe problematic instruction:rn----------rn==> assignment: s=JspTaglibs["/WEB-INF/struts-tags.tld"] [on line 3, column 1 in ok.ftl]rn----------rnrnJava backtrace for programmers:rn----------rnfreemarker.template.TemplateModelException: Could not load taglib informationrn at freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:184)rn at freemarker.core.DynamicKeyName.dealWithStringKey(DynamicKeyName.java:136)rn at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:94)rn at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)rn at freemarker.core.Assignment.accept(Assignment.java:90)rn at freemarker.core.Environment.visit(Environment.java:209)rn at freemarker.core.MixedContent.accept(MixedContent.java:92)rn at freemarker.core.Environment.visit(Environment.java:209)rn at freemarker.core.Environment.process(Environment.java:189)rn at freemarker.template.Template.process(Template.java:237)rn at org.apache.struts2.views.freemarker.FreemarkerResult.doExecute(FreemarkerResult.java:187)rn at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)rn at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)rn at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)rn at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)rn at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)rn at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)rn at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)rn at java.lang.Thread.run(Unknown Source)rnCaused by: java.lang.ClassNotFoundException: org.apache.struts2.views.jsp.ui.AutocompleterTagrn at freemarker.ext.jsp.TaglibFactory$TldParser.endElement(TaglibFactory.java:611)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endNamespaceScope(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)rn at freemarker.ext.jsp.TaglibFactory.parseXml(TaglibFactory.java:384)rn at freemarker.ext.jsp.TaglibFactory.loadTaglib(TaglibFactory.java:452)rn at freemarker.ext.jsp.TaglibFactory.access$300(TaglibFactory.java:106)rn at freemarker.ext.jsp.TaglibFactory$Taglib.load(TaglibFactory.java:414)rn at freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:158)rn ... 71 morernrn 论坛

没有更多推荐了,返回首页