📄 requestutils.java
字号:
if ((period >= 0) && (period > slash)) {
value = value.substring(0, period);
}
if (value.startsWith("/")) {
return (value);
} else {
return ("/" + value);
}
}
/**
* Return the form action converted into a server-relative URL.
*/
public static String getActionMappingURL(String action, PageContext pageContext) {
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
StringBuffer value = new StringBuffer();
String pre = request.getContextPath();
if( ! pre.equals("/")){
value.append(pre);
}
ModuleConfig config =
(ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
if (config != null) {
value.append(config.getPrefix());
}
// Use our servlet mapping, if one is specified
String servletMapping =
(String) pageContext.getAttribute(Globals.SERVLET_KEY, PageContext.APPLICATION_SCOPE);
if (servletMapping != null) {
String queryString = null;
int question = action.indexOf("?");
if (question >= 0) {
queryString = action.substring(question);
}
String actionMapping = getActionMappingName(action);
if (servletMapping.startsWith("*.")) {
value.append(actionMapping);
value.append(servletMapping.substring(1));
} else if (servletMapping.endsWith("/*")) {
value.append(servletMapping.substring(0, servletMapping.length() - 2));
value.append(actionMapping);
} else if (servletMapping.equals("/")) {
value.append(actionMapping);
}
if (queryString != null) {
value.append(queryString);
}
}else {
if (!action.startsWith("/")) {
value.append("/");
}
value.append(action);
}
// Return the completed value
return (value.toString());
}
/**
* Create (if necessary) and return an ActionForm instance appropriate
* for this request. If no ActionForm instance is required, return
* <code>null</code>.
*
* @param request The servlet request we are processing
* @param mapping The action mapping for this request
* @param moduleConfig The configuration for this module
* @param servlet The action servlet
* @return ActionForm instance associated with this request
*/
public static ActionForm createActionForm(
HttpServletRequest request,
ActionMapping mapping,
ModuleConfig moduleConfig,
ActionServlet servlet) {
// Is there a form bean associated with this mapping?
String attribute = mapping.getAttribute();
if (attribute == null) {
return (null);
}
// Look up the form bean configuration information to use
String name = mapping.getName();
FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
if (config == null) {
return (null);
}
// Look up any existing form bean instance
if (log.isDebugEnabled()) {
log.debug(
" Looking for ActionForm bean instance in scope '"
+ mapping.getScope()
+ "' under attribute key '"
+ attribute
+ "'");
}
ActionForm instance = null;
HttpSession session = null;
if ("request".equals(mapping.getScope())) {
instance = (ActionForm) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (ActionForm) session.getAttribute(attribute);
}
// Can we recycle the existing form bean instance (if there is one)?
if (instance != null) {
if (config.getDynamic()) {
String className = ((DynaBean) instance).getDynaClass().getName();
if (className.equals(config.getName())) {
if (log.isDebugEnabled()) {
log.debug(
" Recycling existing DynaActionForm instance "
+ "of type '"
+ className
+ "'");
log.trace(" --> " + instance);
}
return (instance);
}
} else {
try {
Class configClass = applicationClass(config.getType());
if (configClass.isAssignableFrom(instance.getClass())) {
if (log.isDebugEnabled()) {
log.debug(
" Recycling existing ActionForm instance "
+ "of class '"
+ instance.getClass().getName()
+ "'");
log.trace(" --> " + instance);
}
return (instance);
}
} catch (Throwable t) {
log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
return (null);
}
}
}
// Create and return a new form bean instance
if (config.getDynamic()) {
try {
DynaActionFormClass dynaClass =
DynaActionFormClass.createDynaActionFormClass(config);
instance = (ActionForm) dynaClass.newInstance();
((DynaActionForm) instance).initialize(mapping);
if (log.isDebugEnabled()) {
log.debug(
" Creating new DynaActionForm instance "
+ "of type '"
+ config.getType()
+ "'");
log.trace(" --> " + instance);
}
} catch (Throwable t) {
log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
return (null);
}
} else {
try {
instance = (ActionForm) applicationInstance(config.getType());
if (log.isDebugEnabled()) {
log.debug(
" Creating new ActionForm instance "
+ "of type '"
+ config.getType()
+ "'");
log.trace(" --> " + instance);
}
} catch (Throwable t) {
log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
return (null);
}
}
instance.setServlet(servlet);
return (instance);
}
/**
* Locate and return the specified bean, from an optionally specified
* scope, in the specified page context. If no such bean is found,
* return <code>null</code> instead. If an exception is thrown, it will
* have already been saved via a call to <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param scopeName Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
* @return JavaBean in the specified page context
* @exception JspException if an invalid scope name
* is requested
*/
public static Object lookup(PageContext pageContext, String name, String scopeName)
throws JspException {
if (scopeName == null) {
return pageContext.findAttribute(name);
}
try {
return pageContext.getAttribute(name, getScope(scopeName));
} catch (JspException e) {
saveException(pageContext, e);
// log.info( " exception with find bean named "
// +name +":" + e.getMessage());
e.printStackTrace();
return null;
//throw e;
}
}
/**
* Converts the scope name into its corresponding PageContext constant value.
* @param scopeName Can be "page", "request", "session", or "application" in any
* case.
* @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
* @throws JspException if the scopeName is not a valid name.
* @since Struts 1.1
*/
public static int getScope(String scopeName) throws JspException {
Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
if (scope == null) {
throw new JspException(messages.getMessage("lookup.scope", scope));
}
return scope.intValue();
}
/**
* Locate and return the specified property of the specified bean, from
* an optionally specified scope, in the specified page context. If an
* exception is thrown, it will have already been saved via a call to
* <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param property Name of the property to be retrieved, or
* <code>null</code> to retrieve the bean itself
* @param scope Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
* @return property of specified JavaBean
*
* @exception JspException if an invalid scope name
* is requested
* @exception JspException if the specified bean is not found
* @exception JspException if accessing this property causes an
* IllegalAccessException, IllegalArgumentException,
* InvocationTargetException, or NoSuchMethodException
*/
public static Object lookup(
PageContext pageContext,
String name,
String property,
String scope)
throws JspException {
// Look up the requested bean, and return if requested
Object bean = lookup(pageContext, name, scope);
if (bean == null) { //modify by steven
// JspException e = null;
// if (scope == null) {
// e = new JspException(messages.getMessage("lookup.bean.any", name));
// } else {
// e = new JspException(messages.getMessage("lookup.bean", name, scope));
// }
// saveException(pageContext, e);
// throw e;
// if (scope == null) {
// log.info("can not find bean named " + name +" in any scope");
// }else{
// log.info("can not find bean named " + name +" in scope " + scope);
// }
return null;
}
if (property == null) {
return bean;
}
// Locate and return the specified property
try {
// return PropertyUtils.getProperty(bean, property);
// modify by steven ,add support for BaseForm
if( bean != null & bean instanceof BaseForm ){
return (( BaseForm )bean).getValue(property);
}else{
return PropertyUtils.getProperty(bean, property);
}
} catch (IllegalAccessException e) {
saveException(pageContext, e);
throw new JspException(messages.getMessage("lookup.access", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null) {
t = e;
}
saveException(pageContext, t);
throw new JspException(messages.getMessage("lookup.target", property, name));
} catch (NoSuchMethodException e) {
saveException(pageContext, e);
throw new JspException(messages.getMessage("lookup.method", property, name));
}
}
/**
* Look up and return current user locale, based on the specified parameters.
*
* @param pageContext The PageContext associated with this request
* @param locale Name of the session attribute for our user's Locale. If this is
* <code>null</code>, the default locale key is used for the lookup.
* @return current user locale
*/
public static Locale retrieveUserLocale(PageContext pageContext, String locale) {
Locale userLocale = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -