mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
Uses multiple suffixes
This commit is contained in:
+17
-5
@@ -21,8 +21,12 @@
|
||||
package org.apache.struts2.convention;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This class strips the word <b>Action</b> from the end of the class name
|
||||
@@ -33,7 +37,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
* </p>
|
||||
*/
|
||||
public class DefaultActionNameBuilder implements ActionNameBuilder {
|
||||
private String actionSuffix = "Action";
|
||||
private Set<String> actionSuffix = Collections.singleton("Action");
|
||||
private boolean lowerCase;
|
||||
|
||||
@Inject
|
||||
@@ -48,7 +52,7 @@ public class DefaultActionNameBuilder implements ActionNameBuilder {
|
||||
@Inject(value = "struts.convention.action.suffix", required = false)
|
||||
public void setActionSuffix(String actionSuffix) {
|
||||
if (StringUtils.isNotBlank(actionSuffix)) {
|
||||
this.actionSuffix = actionSuffix;
|
||||
this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +60,7 @@ public class DefaultActionNameBuilder implements ActionNameBuilder {
|
||||
String actionName = className;
|
||||
|
||||
// Truncate Action suffix if found
|
||||
if (actionName.endsWith(actionSuffix)) {
|
||||
actionName = actionName.substring(0, actionName.length() - actionSuffix.length());
|
||||
}
|
||||
actionName = truncateSuffixIfMatches(actionName);
|
||||
|
||||
// Force initial letter of action to lowercase, if desired
|
||||
if ((lowerCase) && (actionName.length() > 1)) {
|
||||
@@ -72,4 +74,14 @@ public class DefaultActionNameBuilder implements ActionNameBuilder {
|
||||
|
||||
return actionName;
|
||||
}
|
||||
|
||||
private String truncateSuffixIfMatches(String name) {
|
||||
String actionName = name;
|
||||
for (String suffix : actionSuffix) {
|
||||
if (actionName.endsWith(suffix)) {
|
||||
actionName = actionName.substring(0, actionName.length() - suffix.length());
|
||||
}
|
||||
}
|
||||
return actionName;
|
||||
}
|
||||
}
|
||||
+28
-8
@@ -21,10 +21,14 @@
|
||||
package org.apache.struts2.convention;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This class converts the class name into a SEO friendly name by recognizing
|
||||
@@ -35,7 +39,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
*/
|
||||
public class SEOActionNameBuilder implements ActionNameBuilder {
|
||||
private static final Logger LOG = LogManager.getLogger(SEOActionNameBuilder.class);
|
||||
private String actionSuffix = "Action";
|
||||
private Set<String> actionSuffix = Collections.singleton("Action");
|
||||
private boolean lowerCase;
|
||||
private String separator;
|
||||
|
||||
@@ -53,20 +57,17 @@ public class SEOActionNameBuilder implements ActionNameBuilder {
|
||||
@Inject(value = "struts.convention.action.suffix", required = false)
|
||||
public void setActionSuffix(String actionSuffix) {
|
||||
if (StringUtils.isNotBlank(actionSuffix)) {
|
||||
this.actionSuffix = actionSuffix;
|
||||
this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
public String build(String className) {
|
||||
String actionName = className;
|
||||
|
||||
if (actionName.equals(actionSuffix))
|
||||
throw new IllegalStateException("The action name cannot be the same as the action suffix [" + actionSuffix + "]");
|
||||
|
||||
checkActionName(actionName);
|
||||
|
||||
// Truncate Action suffix if found
|
||||
if (actionName.endsWith(actionSuffix)) {
|
||||
actionName = actionName.substring(0, actionName.length() - actionSuffix.length());
|
||||
}
|
||||
actionName = truncateSuffixIfMatches(actionName);
|
||||
|
||||
// Convert to underscores
|
||||
char[] ca = actionName.toCharArray();
|
||||
@@ -93,4 +94,23 @@ public class SEOActionNameBuilder implements ActionNameBuilder {
|
||||
|
||||
return actionName;
|
||||
}
|
||||
|
||||
void checkActionName(String actionName) {
|
||||
for (String suffix : actionSuffix) {
|
||||
if (actionName.equals(suffix)) {
|
||||
throw new IllegalStateException("The action name cannot be the same as the action suffix [" + suffix + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String truncateSuffixIfMatches(String name) {
|
||||
String actionName = name;
|
||||
for (String suffix : actionSuffix) {
|
||||
if (actionName.endsWith(suffix)) {
|
||||
actionName = actionName.substring(0, actionName.length() - suffix.length());
|
||||
}
|
||||
}
|
||||
return actionName;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user