以前学过Spring、Struts、Hibernate的开发,今天想看看Spring、Struts、Ibatis的框架,本着从头做起的原则,下载一些相关的Jar包,开始做起。
首先,用Myeclipse建一个Web Project。然后导入必要的Jar包,由于之前下的Jar包比较新,Struts2.3的,和以前做的不太一样,导入的时候报错了。然后修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*</url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
然后在src目录下新建struts.xml,添加以下内容:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.ui.theme" value="simple" /> <constant name="struts.multipart.saveDir" value="\temp" /> <constant name="struts.multipart.maxSize" value="3048576000" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="index" namespace="/" extends="struts-default"> <action name="*" class="core.action.UserAction" method="index"> <result name="index">/{1}.jsp</result> </action> </package> </struts>
接着写一个测试的Action,建立core.action包,然后添加UserAction类:
package core.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
public String index() {
return INPUT;
}
}
再之后写一个JSP页面,响应index方法的请求:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>index</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> This is my JSP page. <br> </body> </html>
然后部署, 启动服务器,打开浏览器localhost:8080/项目名/index报错了。
错误如下: Dispatcher initialization failed 错误
于是搜了搜,发现现在的struts2不仅仅是那5个核心包了,还扩展了其他的包,例如Commons-fileupload,Commons-IO包等。现将我整理好的Jar包最小化打包上传,希望遇到类似问题的朋友参考参考。
这回好了,问题解决。一看控制台,出了个提示:
>>> FilterDispatcher <<< is deprecated! Please use the new filters!
一看又是版本问题导致的struts2过时了,修改web.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*</url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
原来从>=2.1.3的版本开始,FilterDispatcher被标记为过时,取而代之的是新的StrutsPrepareAndExecuteFilter。这回搞定了,没有提示错误和警告了。