阅读本文的基础是需要已经懂得在Eclipse下创建动态网站工程并懂得在Eclipse中配置Tomcat。
当然,JDK、Tomcat和Eclipse也得先安装好,在此不具体说明了。
先说一下本文中用到的软件版本:
OS Ubuntu 14.04,Eclipse IDE for JAVA EE V4.4.0,Tomcat 7.0.55,JDK 7
开始:
到Apache官网下载Struts 2,下载地址为:http://struts.apache.org/development/2.x/ ,本文用的版本为2.3.16(写文章时最新版本),用更加新的版本请参考官方文档是否有较大的更新而配置不同。
下载列表中,有几个不同的压缩包,推荐下载”Full Distribution“版,包含了文档和源代码,要深入了解Struts 2这个是必备。下载的是zip压缩包,解压后,里面有四个文件夹:
- apps:官方的例子,可直接参考里面的代码。用Eclipse里的Import功能可导入File->Import…->Web->WAR file->Next->选择文件就可以导入了。
- docs:官方文档,可直接用浏览器浏览。
- lib:我们开发需要用到的库,都是jar。
- src:所有的Struts 2中的源代码。
打开Eclipse,
图1
如图1所示,选择”Dynamic Web Project“创建一个动态网站项目
然后会弹出如图2 窗口
图2
在1处填入你的项目名称,在2处选中你配置好的Tomcat,如果你没有配置,请点击3来新建。(本文不包括Eclipse的配置说明,请查阅其他资料。)然后点最方的”Next>“按钮。
会显示这样一个窗口
图3
继续点”Next>“
图4
然后勾上图4所示的选项,生成web.xml文件(关于web.xml的说明请参考http://www.cnblogs.com/hellojava/archive/2012/12/28/2835730.html 这里暂不阐述),点”Finish“后创建完成。
图5
看看图4的目录结构,src用来保存java文件,/WebContent里放除了java文件以外的文件,当然包括jar文件(放到lib内)。
如果你当前不是Java EE视图的话,要切换一下。
在Eclipsh的右上角可看到Java EE按钮,如果当前选中的不是Java EE请点击一下。
图6
我们先来配置一下web.xml,打开web.xml文件
图7
这个是Eclipse创建的内容,welcome-file-list配置的是这个项目的欢迎页面,就是当你访问本网站的根目录的时候,会自动打开这里配置的文件,优先级由上往下。可以先不管这个。
在最后的</web-app>前插入以下代码
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<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> |
注意,有些旧版的书上用的struts2的web.xml配置可能不一样,请参考:http://struts.apache.org/release/2.1.x/docs/webxml.html
然后把之前下载的struts2解压出来的lib文件夹里的必要jar文件复制到项目的lib中,必要的文件如下(注:如果你下载的struts2的版本与本文的不一致,下方文件的版本号可能也会不一致)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang3-3.1.jar freemarker-2.3.19.jar javassist-3.11.0.GA.jar ognl-3.0.6.jar struts2-core-2.3.16.3.jar xwork-core-2.3.16.3.jar |
然后在Eclipse下方
图8
点击Servers选项卡
在你的服务器上点击右键
图9
点击 ”Add and Remove…“,
图10
选中你的项目,点Add添加到右边,点”Finish“确定。
然后可在两个地方开启服务器,但只要点一个就可以:
图11
然后Console会输出启动信息,如果有异常可以在Console看到严重的异常。
如果你按本文章创建的步骤,一般不会出现什么问题,如果你真的出现问题,先看看异常信息中的如下错误:
java.lang.ClassNotFoundException : *******
一般来说这个是缺少某个jar包,可看*处的显示按需添加。
至于其他错误,具体问题具体分析。
如果你上述的操作都没问题的话,我们下面来试试struts2到底能不能工作了。
我们做一个简单的登录跳转的功能。
首先在src中新建一个类LoginAction,继承ActionSupport
代码如下:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class LoginAction extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception { if(getUsername().equals("test") && getPassword().equals("test")) { ActionContext.getContext().getSession().put("user", username); return SUCCESS; } else { return ERROR; } } } |
再在src中新建一个struts.xml文件
代码如下
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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 是 Struts 2 配置文件的根元素 --> <struts> <!-- Struts 2 的Action必须放在指定的包空间下定义 --> <package name="strutsqs" extends="struts-default"> <!-- 定义login的Action,该Action的实现类为hao.LoginAction类 --> <action name="login" class="hao.LoginAction"> <!-- 定义处理结果和视图资源之间映射关系 --> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> </package> </struts> |
其中“hao.LoginAction”中的hao是LoginAction的包
然后在WebContent下创建三个jsp文件,文件如下:
index.jsp
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <s:form action="login"> <s:textfield name="username" key="user"/> <s:textfield name="password" key="pass"/> <s:submit value="登录"/> </s:form> </body> </html> |
welcome.jsp
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 欢迎,${sessionScope.user},你已经登录 </body> </html> |
error.jsp
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> error </body> </html> |
最后重启一下服务器,在浏览器里访问:http://localhost:8080/struts2-demo/index.jsp
在页面中输入用户名:test 和密码:test 登录,或者输入其他用户名密码,分别跳转到两个页面,第一个是welcome.jsp,第二个是error.jsp。
如果你的效果也一样,恭喜你,已经完成。