古老的技术栈:struts2+maven+freemaker+jsp,主要为了学习freemaker模板引擎

关于freemaker的使用

一.简介

二.使用

  1. 引入对应的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>iris-rsinte-client</artifactId>
        <groupId>com.iris</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.iris</groupId>
    <artifactId>rsinte-client</artifactId>
    <version>2.0.6-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <java.version>1.6</java.version>
        <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
        <maven.jar.plugin.version>2.3.1</maven.jar.plugin.version>
        <maven.source.plugin.version>2.1.2</maven.source.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.1</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.35</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.2.1</version>
        </dependency>


        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.2</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!-- 指定编译的jdk使用版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>

            <!-- 利用Maven的Source插件,对Maven工程的源码进行打jar包,安装时,会同时将源码包安装到本地仓库 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>

        <!-- 默认情况下,打包的时候src文件下的java文件会被编译成.class文件放于target下,其它的例如tld文件是不会被打包放在target下的
         因此,这个resource的目的是为了在打包的时候,指定包含对应的文件也要给我放到target文件夹下
         -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.class</include>
                    <include>**/*.ftl</include>
                </includes>
            </resource>
            <resource>
                <targetPath>META-INF</targetPath>
                <directory>META-INF</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
    </build>


</project>
  1. 编写自定义标签 .tld文件(tld文件放在webapp/WEB-INF文件夹下)
<?xml version="1.0" encoding="UTF-8"?>

<taglib 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-jsptaglibrary_2_1.xsd"
        version="2.1">
    <description>自定义标签库</description>
    <display-name>estl core</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>eas</short-name>
    <uri>/yzy</uri>
    <tag>
        <name>Hello</name>
        <tag-class>freemakerTest.TestFreemaker</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
  1. 编写tld文件<tag-class>标签对应的TestFreemaker.java文件
package freemakerTest;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class TestFreemaker extends BodyTagSupport {
    private String name = "";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int doStartTag() throws JspException {
        Configuration config = new Configuration();
        try {
            Map<String, Object> rootMap = new HashMap<String, Object>();
            rootMap.put("name", name);
            TemplateLoader templateLoader =  new ClassTemplateLoader(this.getClass(),"template/");
            config.setTemplateLoader(templateLoader);
            Template template = config.getTemplate("freemarker-demo.ftl", "utf-8");
            template.process(rootMap, pageContext.getOut());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;
    }
}

  1. 编写freemarker-demo.ftl文件(注意:该文件位于TestFreemaker.java文件的同级目录的template包下),例如我的:
    在这里插入图片描述
<h1>我是ftl的内容--${name}</h1>
  1. 编写jsp页面
    添加自定义标签:<%@ taglib prefix=“z” uri="/yzy" %>
    使用:<z:Hello name=“aaa”></z:Hello>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="z" uri="/yzy" %>
<html>
<head>
    <title>Title</title>

</head>
<body>
<h2>测试页面---</h2>
<z:Hello name="寒冰---我的值会在后台获取到的哦,然后再赋值给flt文件对应的地方,请注意"></z:Hello>
</body>
</html>

三. 效果
在这里插入图片描述
四. 使用场景举例子

  1. 利用其为页面标签如input输入框的value进行加密
    a. 引入依赖
		<dependency>
            <groupId>net.iharder</groupId>
            <artifactId>base64</artifactId>
            <version>2.3.8</version>
        </dependency>
    b. 修改index.jsp,添加
<input id="code" style="width:300px;" name="code" value="<z:code value='123'></z:code>" >
	c. 修改test.tld文件,添加
	<tag>
        <name>code</name>
        <tag-class>freemakerTest.code</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
	d. 添加code.java文件
package freemakerTest;

import net.iharder.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.security.GeneralSecurityException;

public class code extends BodyTagSupport {
    private String value = "";

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    // 加密:
    public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKey keySpec = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        return cipher.doFinal(input);
    }

    // 解密:
    public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKey keySpec = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        return cipher.doFinal(input);
    }

    @Override
    public int doStartTag() throws JspException {
        try {
            String message = value;
            if (StringUtils.isEmpty(message)){
                pageContext.getOut().write("");
                return  EVAL_BODY_INCLUDE;
            }
            byte[] key = "ilyzxy8952012356".getBytes("UTF-8"); // 128位密钥 = 16 bytes Key:
            byte[] data = message.getBytes("UTF-8");
            byte[] encrypted = encrypt(key, data);// 加密,得到加密后的字节数组
            String s = Base64.encodeBytes(encrypted);// 把加密的字节数组转为字符串

//            byte[] decode = Base64.decode(s);// 根据加密的字符串得到加密的字节数组
//            byte[] decrypted = decrypt(key, decode);// 解密
//            System.out.println("解密: " + new String(decrypted, "UTF-8"));
            pageContext.getOut().write(s);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;
    }
}

	e. 页面效果

在这里插入图片描述
到此,jsp使用自定义标签实现信息加密完成,要进行解密的话,上面也有解密的代码,根据业务场景自己套用就好啦

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值