简体   繁体   English

如何在Struts2中子字符串化?

[英]How to substring in Struts2?

How can I substring a string using Struts2 taglibs? 如何使用Struts2标签库对字符串进行子字符串化?

This is my attempt using JSTL/EL: 这是我使用JSTL / EL的尝试:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<s:property value="firstName" />
<c:set var="string" value="${firstName} "/>
<c:out value="${fn:substring(string,0,5)} "/>

This does however not work. 但是,这不起作用。 How can I achieve my requirement? 如何达到我的要求?

假设firstName是一个java.lang.String,那么:

<s:property value="firstName.substring(0,5)" />

The substring function works only on the underlying Java String object and not on the s:set variable we made of it. substring函数仅在基础Java String对象上起作用,而在我们由其构成的s:set变量上不起作用。 For example: 例如:

Suppose I have a (Action)class which contains a Java variable email. 假设我有一个(Action)类,其中包含一个Java变量email。 Then I can access this variable in the JSP file like this: 然后,我可以像这样在JSP文件中访问此变量:

<s:set name="jspEmail" value="%{email}" />

If I want now to substring everything before the @, I have to do this on the Java variable AND NOT on the JSP struts variable. 如果现在我想对@之前的所有子字符串进行子字符串化,则必须在Java变量而不是JSP struts变量上执行此操作。 So like this: 像这样:

<s:set name="namepart" value="%{email.substring(0,email.indexOf("@"))}" />

and then use it like: 然后像这样使用它:

<s:property value="%{namepart}"/>

您可以使用JSP EL作为${action.property}引用动作属性。

<c:out value="${fn:substring(action.firstName, 0, 5)} "/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM