最近搭建了一个websphere5.1+spring2.0+struts2+hibernate3的框架,因为jdk1.4加websphere5.1环境中间碰到一些问题,网上的解决方案也不全,今天搞了一整天总算搞定了,现与大家分享下。
开发工具myeclipse6,先在tomcat4 jdk4上测试,然后部署到websphere5.1上测试通过。
步骤:
1.设置编译环境为jdk1.4
2.把自己代码中所有1.5的特性都改成1.4
3.web.xml改成servlet2.3规范的
4.lib包不要配缺了就行
spring和hibernate的包都是jdk1.4编译的,不需要修改,其中cglib用的是cglib-nodep-2.1_3.jar而不是cglib-2.1_3.jar
struts2由于是jdk1.5编译的需要用retrotranslator转换,工具在struts2目录下的backport目录中,struts2-core-j4-2.0.11.1.jar和xwork-j4-2.0.4.jar已经转好了,再自己转个struts2-spring-plugin-j4-2.0.11.1.jar就可以了,最后把backport-util-concurrent-3.0.jar,retrotranslator-runtime-1.2.2.jar和你转好的包扔到lib目录下,原来的删除。
这方面网上资料还是蛮多的,不明白的可以查找下。
5.websphere5.1兼容性问题
当你成功完成上述步骤后tomcat4已经可以跑了,然后部署到websphere5.1如果没有错误的话可以运行而且后台不报错,但是页面上报XXX.action路径找不到,这个花了我好多时间才搞定,网上也没找到具体解决方案。
原因:
DefaultActionMapper类中的getUri方法:
String getUri(HttpServletRequest request) { // handle http dispatcher includes. String uri = (String) request .getAttribute("javax.servlet.include.servlet_path"); if (uri != null) { return uri; } uri = RequestUtils.getServletPath(request); //System.out.println("url==========="+uri); //old:if (uri != null && !"".equals(uri)) { if (uri != null && uri.length() > 1) { return uri; } uri = request.getRequestURI(); return uri.substring(request.getContextPath().length()); }
发现上面注释掉的那句:
//System.out.println("url==========="+uri);
输出为"/",实际为"/XX/XX.action"看来是uri解析错误,猜想可能是
RequestUtils.getServletPath(request);
这个方法有bug,结果发现源代码没提供,所以只能在外层方法中改下了,重写了个过滤器。改完后成功运行。
解决方案如下,添加2个类
DefaultActionMapperCompaWebsphere51(修改了getUri后的判断)
/* * $Id: DefaultActionMapper.java 540141 2007-05-21 13:46:48Z mrdon $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.struts2.dispatcher.mapper; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.RequestUtils; public class DefaultActionMapperCompaWebsphere51 extends DefaultActionMapper { private static ActionMapper actionMapper = new DefaultActionMapperCompaWebsphere51(); public static ActionMapper getActionMapper(){ return actionMapper; } /** * modify uri compare * @param request * @return */ String getUri(HttpServletRequest request) { // handle http dispatcher includes. String uri = (String) request .getAttribute("javax.servlet.include.servlet_path"); if (uri != null) { return uri; } uri = RequestUtils.getServletPath(request); //System.out.println("url==========="+uri); //old:if (uri != null && !"".equals(uri)) { if (uri != null && uri.length() > 1) { return uri; } uri = request.getRequestURI(); return uri.substring(request.getContextPath().length()); } }
FilterDispatcherCompaWebsphere51(调用自己刚才重写的DefaultActionMapperCompaWebsphere51)
/* * $Id: FilterDispatcherCompatWeblogic61.java 476075 2006-11-17 08:28:30Z mrdon $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.struts2.dispatcher; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.RequestUtils; import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.apache.struts2.dispatcher.mapper.DefaultActionMapperCompaWebsphere51; import com.opensymphony.xwork2.util.profiling.UtilTimerStack; public class FilterDispatcherCompaWebsphere51 extends FilterDispatcher { private static boolean serveStatic; public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; ServletContext servletContext = getServletContext(); String timerKey = "FilterDispatcher_doFilter: "; try { UtilTimerStack.push(timerKey); request = prepareDispatcherAndWrapRequest(request, response); ActionMapping mapping; ActionMapper actionMapper = DefaultActionMapperCompaWebsphere51.getActionMapper(); try { mapping = actionMapper.getMapping(request, dispatcher .getConfigurationManager()); } catch (Exception ex) { log.error("error getting ActionMapping", ex); dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); return; } if (mapping == null) { // there is no action in this request, should we look for a // static resource? String resourcePath = RequestUtils.getServletPath(request); if ("".equals(resourcePath) && null != request.getPathInfo()) { resourcePath = request.getPathInfo(); } if (serveStatic && resourcePath.startsWith("/struts")) { String name = resourcePath.substring("/struts".length()); findStaticResource(name, request, response); } else { // this is a normal request, let it pass through chain.doFilter(request, response); } // The framework did its job here return; } dispatcher .serviceAction(request, response, servletContext, mapping); } finally { try { ActionContextCleanUp.cleanUp(req); } finally { UtilTimerStack.pop(timerKey); } } } private static final Log log = LogFactory .getLog(FilterDispatcherCompaWebsphere51.class); }
修改web.xml中原来的struts2的filter类
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcherCompaWebsphere51
</filter-class>
</filter>
之后就可以放在tomcat和websphere5.1上运行了。
本文只说明了遇到的一些问题的解决方案,如何配置struts2或spring或hibernate可以参考官方资料。
如有不明白的可以留言。