请教一个struts方面的问题 呃,我记得一本书上也是这么说的,但是我一般是用<html:link/> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我认为,编程应遵循"最简"原则,能简单就简单,所以在这里能直接转就直接转。“加一个action去找forward”的目的是为要通过action加载数据,并不是为了找forward。如果不用加载Bean数据,直接接转到JSP页也是好的。下面是相关信息:********************************为了了解楼主所说这一问题,我花了一翻功夫来收集了Struts自带的例子中<html:link >的用法。结果如下:<html:link action="/Logon"> <bean:message key="change.try"/></html:link>----------<html:link action="/EditRegistration?action=Edit"><bean:message key="mainMenu.registration"/></html:link>------------<html:link forward="logoff"><bean:message key="mainMenu.logoff"/></html:link>---------<html:link action="/EditRegistration?action=Create"><bean:message key="index.registration"/></html:link>------------<html:link action="/Logon"><bean:message key="index.logon"/></html:link>------------<html:link action="/Locale?language=en">English</html:link><html:link action="/Locale?language=ja" useLocalEncoding="true">Japanese</html:link><html:link action="/Locale?language=ru" useLocalEncoding="true">Russian</html:link>------------<html:link action="/Tour"><bean:message key="index.tour"/></html:link>--------***********************下面是<html:link>标签处理forward、href、page、action等参数的方法,实际上都只是生成link连接字符串:public static String computeURL( PageContext pageContext, String forward, String href, String page, String action, Map params, String anchor, boolean redirect) throws MalformedURLException { // Validate that exactly one specifier was included int n = 0; if (forward != null) { n++; } if (href != null) { n++; } if (page != null) { n++; } if (action != null) { n++; } if (n != 1) { throw new MalformedURLException(messages.getMessage("computeURL.specifier")); } // Look up the module configuration for this request ModuleConfig config = (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY); if (config == null) { // Backwards compatibility hack config = (ModuleConfig) pageContext.getServletContext().getAttribute(Globals.MODULE_KEY); pageContext.getRequest().setAttribute(Globals.MODULE_KEY, config); } // Calculate the appropriate URL StringBuffer url = new StringBuffer(); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); if (forward != null) { ForwardConfig fc = config.findForwardConfig(forward); if (fc == null) { throw new MalformedURLException(messages.getMessage("computeURL.forward", forward)); } if (fc.getRedirect()) { redirect = true; } if (fc.getPath().startsWith("/")) { url.append(request.getContextPath()); url.append(forwardURL(request, fc)); } else { url.append(fc.getPath()); } } else if (href != null) { url.append(href); } else if (action != null) { url.append(getActionMappingURL(action, pageContext)); } else /* if (page != null) */ { url.append(request.getContextPath()); url.append(pageURL(request, page)); } // Add anchor if requested (replacing any existing anchor) if (anchor != null) { String temp = url.toString(); int hash = temp.indexOf('#'); if (hash >= 0) { url.setLength(hash); } url.append('#'); url.append(encodeURL(anchor)); } // Add dynamic parameters if requested if ((params != null) && (params.size() > 0)) { // Save any existing anchor String temp = url.toString(); int hash = temp.indexOf('#'); if (hash >= 0) { anchor = temp.substring(hash + 1); url.setLength(hash); temp = url.toString(); } else { anchor = null; } // Define the parameter separator String separator = redirect ? "&" : "&"; // Add the required request parameters boolean question = temp.indexOf('?') >= 0; Iterator keys = params.keySet().iterator(); while (keys.hasNext()) { String key = (String) keys.next(); Object value = params.get(key); if (value == null) { if (!question) { url.append('?'); question = true; } else { url.append(separator); } url.append(encodeURL(key)); url.append('='); // Interpret null as "no value" } else if (value instanceof String) { if (!question) { url.append('?'); question = true; } else { url.append(separator); } url.append(encodeURL(key)); url.append('='); url.append(encodeURL((String) value)); } else if (value instanceof String[]) { String values[] = (String[]) value; for (int i = 0; i < values.length; i++) { if (!question) { url.append('?'); question = true; } else { url.append(separator); } url.append(encodeURL(key)); url.append('='); url.append(encodeURL(values[i])); } } else /* Convert other objects to a string */ { if (!question) { url.append('?'); question = true; } else { url.append(separator); } url.append(encodeURL(key)); url.append('='); url.append(encodeURL(value.toString())); } } // Re-add the saved anchor (if any) if (anchor != null) { url.append('#'); url.append(encodeURL(anchor)); } } // Perform URL rewriting to include our session ID (if any) if (pageContext.getSession() != null) { HttpServletResponse response = (HttpServletResponse) pageContext.getResponse(); if (redirect) { return (response.encodeRedirectURL(url.toString())); } else { return (response.encodeURL(url.toString())); } } else { return (url.toString()); } } 呵呵,也在搞struts,不过刚开始 原因应该是比较清楚的,就是配置的forward找不到合适的路径。可能是因为你的tileDefinitions配置有些什么问题。 找不到message是不是找不到"><html:message key="addmessage"/>这个里面的addmessage呀?你换成别的试试? 因为在STRUTS结构中页面跳转也代表了业务逻辑,业务逻辑和表现层:JSP分开,所以不是简单的页面跳转 同意kui(kui)的观点,如果没有业务逻辑,直接跳转到jsp就可以了 大家帮忙看下这是个什么原因造成的。 hibernate如何处理并发 提问几个JSP的菜鸟问题 求emf-sdo-xsd-SDK插件的下载地址! Framework方向调查JSF,Struts2,Webwork2,Spring 谁有《Tomcat权威指南中文版》啊 cmp中的find方法的问题 用过Struts翻页的朋友请进。 在SOAP JAVA 中 传输Object的数组,应该加什么样的MAPPING 谁能给我 怎么用jbuilder8+jboss3.2.5做程序的调试(*.java,*.jsp) 为什么eclipse3的windows-perfermence下没有tomcat阿?
“加一个action去找forward”的目的是为要通过action加载数据,并不是为了找forward。如果不用加载Bean数据,直接接转到JSP页也是好的。
下面是相关信息:
********************************为了了解楼主所说这一问题,我花了一翻功夫来收集了Struts自带的例子中<html:link >的用法。结果如下:
<html:link action="/Logon">
<bean:message key="change.try"/>
</html:link>
----------
<html:link action="/EditRegistration?action=Edit"><bean:message key="mainMenu.registration"/></html:link>
------------
<html:link forward="logoff"><bean:message key="mainMenu.logoff"/></html:link>
---------
<html:link action="/EditRegistration?action=Create"><bean:message key="index.registration"/></html:link>
------------
<html:link action="/Logon"><bean:message key="index.logon"/></html:link>
------------
<html:link action="/Locale?language=en">English</html:link>
<html:link action="/Locale?language=ja" useLocalEncoding="true">Japanese</html:link>
<html:link action="/Locale?language=ru" useLocalEncoding="true">Russian</html:link>
------------
<html:link action="/Tour"><bean:message key="index.tour"/></html:link>
--------***********************
下面是<html:link>标签处理forward、href、page、action等参数的方法,实际上都只是生成link连接字符串:
public static String computeURL(
PageContext pageContext,
String forward,
String href,
String page,
String action,
Map params,
String anchor,
boolean redirect)
throws MalformedURLException { // Validate that exactly one specifier was included
int n = 0;
if (forward != null) {
n++;
}
if (href != null) {
n++;
}
if (page != null) {
n++;
}
if (action != null) {
n++;
}
if (n != 1) {
throw new MalformedURLException(messages.getMessage("computeURL.specifier"));
} // Look up the module configuration for this request
ModuleConfig config =
(ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
if (config == null) { // Backwards compatibility hack
config =
(ModuleConfig) pageContext.getServletContext().getAttribute(Globals.MODULE_KEY);
pageContext.getRequest().setAttribute(Globals.MODULE_KEY, config);
} // Calculate the appropriate URL
StringBuffer url = new StringBuffer();
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
if (forward != null) {
ForwardConfig fc = config.findForwardConfig(forward);
if (fc == null) {
throw new MalformedURLException(messages.getMessage("computeURL.forward", forward));
}
if (fc.getRedirect()) {
redirect = true;
}
if (fc.getPath().startsWith("/")) {
url.append(request.getContextPath());
url.append(forwardURL(request, fc));
} else {
url.append(fc.getPath());
}
} else if (href != null) {
url.append(href);
} else if (action != null) {
url.append(getActionMappingURL(action, pageContext)); } else /* if (page != null) */ {
url.append(request.getContextPath());
url.append(pageURL(request, page));
} // Add anchor if requested (replacing any existing anchor)
if (anchor != null) {
String temp = url.toString();
int hash = temp.indexOf('#');
if (hash >= 0) {
url.setLength(hash);
}
url.append('#');
url.append(encodeURL(anchor));
} // Add dynamic parameters if requested
if ((params != null) && (params.size() > 0)) { // Save any existing anchor
String temp = url.toString();
int hash = temp.indexOf('#');
if (hash >= 0) {
anchor = temp.substring(hash + 1);
url.setLength(hash);
temp = url.toString();
} else {
anchor = null;
} // Define the parameter separator
String separator = redirect ? "&" : "&"; // Add the required request parameters
boolean question = temp.indexOf('?') >= 0;
Iterator keys = params.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
Object value = params.get(key);
if (value == null) {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(encodeURL(key));
url.append('='); // Interpret null as "no value"
} else if (value instanceof String) {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
url.append(encodeURL((String) value));
} else if (value instanceof String[]) {
String values[] = (String[]) value;
for (int i = 0; i < values.length; i++) {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
url.append(encodeURL(values[i]));
}
} else /* Convert other objects to a string */ {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
url.append(encodeURL(value.toString()));
}
} // Re-add the saved anchor (if any)
if (anchor != null) {
url.append('#');
url.append(encodeURL(anchor));
} } // Perform URL rewriting to include our session ID (if any)
if (pageContext.getSession() != null) {
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
if (redirect) {
return (response.encodeRedirectURL(url.toString()));
} else {
return (response.encodeURL(url.toString()));
}
} else {
return (url.toString());
} }