当前位置:   article > 正文

maven创建webapp+Freemarker组件的实现

maven创建webapp+Freemarker组件的实现

下载安装配置maven

Maven官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘123云盘为您提供Maven最新版正式版官方版绿色版下载,Maven安卓版手机版apk免费下载安装到手机,支持电脑端一键快捷安装icon-default.png?t=N7T8https://www.123pan.com/s/9QRqVv-TcUY.html链接为3.6.2-3.6.3的版本

下载解压其中一个版本

开始配置环境变量

 %MAVEN_HOME%

找到对应的路径

xxxx/bin

系统变量PATH中引入

%MAVEN_HOMOE%

win+r输入cmd后运行命令mvn - v,如图

创建项目

打开idea->新建项目->maven

 

可以自行修改组

com.xxxx

 改为1.8

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. <maven.compiler.target>1.8</maven.compiler.target>
  5. </properties>

导入依赖

  1. <dependency>
  2. <groupId>org.freemarker</groupId>
  3. <artifactId>freemarker</artifactId>
  4. <version>2.3.23</version>
  5. </dependency>
  6. <!-- servlet-api的坐标依赖-->
  7. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  8. <dependency>
  9. <groupId>javax.servlet</groupId>
  10. <artifactId>javax.servlet-api</artifactId>
  11. <version>3.1.0</version>
  12. <scope>provided</scope>
  13. </dependency>

导入组件

  1. <build>
  2. <finalName>F01</finalName>
  3. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  4. <plugins>
  5. <plugin>
  6. <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin -->
  7. <groupId>org.eclipse.jetty</groupId>
  8. <artifactId>jetty-maven-plugin</artifactId>
  9. <version>9.2.1.v20140609</version>
  10. </plugin>
  11. </plugins>
  12. </pluginManagement>
  13. </build>

创建servlet

  1. package com.xxxx;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. @WebServlet("/f001")
  9. public class Freemarker001 extends HttpServlet {
  10. @Override
  11. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12. req.setAttribute("msg","Hello Freemarker!");
  13. req.getRequestDispatcher("template/f001.ftl").forward(req,resp);
  14. }
  15. }

web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <servlet>
  7. <servlet-name>freemarker</servlet-name>
  8. <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
  9. <init-param>
  10. <param-name>TemplatePath</param-name>
  11. <param-value>/</param-value>
  12. </init-param>
  13. <init-param>
  14. <param-name>default_encoding</param-name>
  15. <param-value>UTF-8</param-value>
  16. </init-param>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>freemarker</servlet-name>
  20. <url-pattern>*.ftl</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

创建template目录已经ftl文件

运行

 jetty:run -Djetty.port=9099

访问地址

最后初五,迎财神!

同时,祝天下有情人终成眷属!

@ky 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/130673
推荐阅读
相关标签
  

闽ICP备14008679号