seasar2で使用の「<%@taglib prefix="f" uri="http://sastruts.seasar.org/functions" %>」 「${f:url('~')}」cloneを作成。
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="f" uri="http://sastruts.seasar.org/functions" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="shortcut icon" href="${f:url('/ico/favicon.ico')}" type="image/x-icon" /> <link rel="icon" href="${f:url('/ico/favicon.ico')}" type="image/x-icon" /> <link rel="stylesheet" href="${f:url('/css/jquery-ui.custom.css')}" type="text/css" /> <link rel="stylesheet" href="${f:url('/css/common.css')}" type="text/css" /> <script type="text/javascript" src="${f:url('/js/jquery-2.2.4.min.js')}"></script> </head> :
参考url
https://pgbox.grush.jp/ref/java/jee/jsp/elFunction.html
step1 / 3 - S2Functions.java の クローンを作成
sa-struts/S2Functions.java at master · seasarorg/sa-struts · GitHub の内、 必要なmethodを jp.co.sexy.bknkoutei.cloneseasar.S2Functions として作成
package jp.co.sexy.bknkoutei.cloneseasar; import java.util.Arrays; public class S2Functions { private static final int HIGHEST_SPECIAL = '>'; private static String CONTEXT_PATH = ""; private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][]; static { specialCharactersRepresentation['&'] = "&".toCharArray(); specialCharactersRepresentation['<'] = "<".toCharArray(); specialCharactersRepresentation['>'] = ">".toCharArray(); specialCharactersRepresentation['"'] = """.toCharArray(); specialCharactersRepresentation['\''] = "'".toCharArray(); } /** * HTMLをエスケープします。 * * @param input * 入力値 * @return エスケープした結果 */ public static String h(Object input) { if (input == null) { return ""; } String str = ""; if (input.getClass().isArray()) { Class<?> clazz = input.getClass().getComponentType(); if (clazz == String.class) { str = Arrays.toString((Object[]) input); } else if (clazz == boolean.class) { str = Arrays.toString((boolean[]) input); } else if (clazz == int.class) { str = Arrays.toString((int[]) input); } else if (clazz == long.class) { str = Arrays.toString((long[]) input); } else if (clazz == byte.class) { str = Arrays.toString((byte[]) input); } else if (clazz == short.class) { str = Arrays.toString((short[]) input); } else if (clazz == float.class) { str = Arrays.toString((float[]) input); } else if (clazz == double.class) { str = Arrays.toString((double[]) input); } else if (clazz == char.class) { str = Arrays.toString((char[]) input); } else { str = Arrays.toString((Object[]) input); } } else { str = input.toString(); } return escape(str); } /** * 文字列をHTMLエスケープします。 * * @param buffer * 文字列 * @return エスケープした結果 */ public static String escape(String buffer) { int start = 0; int length = buffer.length(); char[] arrayBuffer = buffer.toCharArray(); StringBuilder escapedBuffer = null; for (int i = 0; i < length; i++) { char c = arrayBuffer[i]; if (c <= HIGHEST_SPECIAL) { char[] escaped = specialCharactersRepresentation[c]; if (escaped != null) { if (start == 0) { escapedBuffer = new StringBuilder(length + 5); } if (start < i) { escapedBuffer.append(arrayBuffer, start, i - start); } start = i + 1; escapedBuffer.append(escaped); } } } if (start == 0) { return buffer; } if (start < length) { escapedBuffer.append(arrayBuffer, start, length - start); } return escapedBuffer.toString(); } /** * URLを計算します。 * * @param input * 入力値 * @return エスケープした結果 */ public static String url(String input) { return CONTEXT_PATH + input; } // public static String url(String input) { // String contextPath = RequestUtil.getRequest().getContextPath(); // StringBuilder sb = new StringBuilder(); // if (contextPath.length() > 1) { // sb.append(contextPath); // } // if (StringUtil.isEmpty(input)) { // sb.append(ActionUtil.calcActionPath()); // } else if (!input.startsWith("/")) { // sb.append(ActionUtil.calcActionPath()).append(input); // } else { // String[] names = StringUtil.split(input, "/"); // S2Container container = SingletonS2ContainerFactory.getContainer(); // StringBuilder sb2 = new StringBuilder(50); // String input2 = input; // for (int i = 0; i < names.length; i++) { // if (container.hasComponentDef(sb2 + names[i] + "Action")) { // String actionPath = RoutingUtil.getActionPath(names, i); // String paramPath = RoutingUtil.getParamPath(names, i + 1); // if (StringUtil.isEmpty(paramPath)) { // input2 = actionPath + "/"; // break; // } // } // sb2.append(names[i] + "_"); // } // sb.append(input2); // } // return ResponseUtil.getResponse().encodeURL(sb.toString()); // } }
step2 / 3 - src/main/webapp/WEB-INF/f.tld 作成
先程のS2Functions.java 同様、 sa-struts/f.tld at master · seasarorg/sa-struts · GitHub の内、 必要なmethod を f.tld に作成
<?xml version="1.0" encoding="UTF-8" ?> <taglib 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-jsptaglibrary_2_0.xsd" version="2.0"> <description>JSTL 1.1 functions library</description> <display-name>JSTL functions</display-name> <tlib-version>1.1</tlib-version> <short-name>f</short-name> <uri>http://sastruts.seasar.org/functions</uri> <function> <description> Escapes characters that could be interpreted as HTML. </description> <name>h</name> <function-class>jp.co.sexy.bknkoutei.cloneseasar.S2Functions</function-class> <function-signature>java.lang.String h(java.lang.Object)</function-signature> <example> ${f:h(param:info)} </example> </function> <function> <description> Calculates URL. </description> <name>url</name> <function-class>jp.co.sexy.bknkoutei.cloneseasar.S2Functions</function-class> <function-signature>java.lang.String url(java.lang.String)</function-signature> <example> <a href="${f:url(param:info)}" ... </example> </function> </taglib>
step 3 / 3 - *.jspで使用
後は、jsp で使用できます。
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="f" uri="http://sastruts.seasar.org/functions" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="shortcut icon" href="${f:url('/ico/favicon.ico')}" type="image/x-icon" /> <link rel="icon" href="${f:url('/ico/favicon.ico')}" type="image/x-icon" /> <link rel="stylesheet" href="${f:url('/css/jquery-ui.custom.css')}" type="text/css" /> <link rel="stylesheet" href="${f:url('/css/common.css')}" type="text/css" /> <script type="text/javascript" src="${f:url('/js/jquery-2.2.4.min.js')}"></script> </head> :