使用eclipse创建Struts2项目
eclipse版本: Kepler Service Release 1??? http://www.eclipse.org/downloads/
struts版本:2.3.16???? http://struts.apache.org/
1.新建web項目
打開Eclipse,新建一個web項目"Struts2"
項目名字
勾選 web.xml選項
建好的項目
2.拷貝struts的jar包
在struts2文件包中,找到struts-2.3.16\apps\struts2-blank.war文件,并將其用解壓,這個項目完全是一個空白項目。
復制\WEB-INF\lib\ 下的所有jar包
將前面復制的所有jar包復制到eclipse所建立的項目 -- WebContent -- WEB-INF --lib下
已經復制成功后的lib目錄視圖
在Libraries下的Web App Libraries下也可以引用成功的jar包
3.拷貝web.xml
在struts的WEB-INF下有web.xml,可以直接拷貝到Eclipse的WEB-INF下,也可以只取其中的filter復制到Eclipse中已經的web.xml
拷貝filter
下面是一個完整的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>Struts2</display-name><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><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>
4.拷貝Struts.xml文件以及相關的java文件和其他的xml配置文件
復制struts2-blank\WEB-INF\src\java目錄下的所有文件
因為需要在Package Explorer中進行粘貼,在Project Explorer中不能正確粘貼。
所有先要切換到Package Explorer
拷貝好Struts.xml文件以及相關的java文件以后的視圖
struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="false" /><constant name="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><default-action-ref name="index" /><global-results><result name="error">/error.jsp</result></global-results><global-exception-mappings><exception-mapping exception="java.lang.Exception" result="error"/></global-exception-mappings><action name="index"><result type="redirectAction"><param name="actionName">HelloWorld</param><param name="namespace">/example</param></result></action></package><include file="example.xml"/><!-- Add packages here --></struts>
4.拷貝jsp文件
選擇struts2-blank目錄下的index.htm文件以及jsp目錄
復制上述文件到WebContent下
5.啟動項目
在需要啟動的文件上右鍵點擊運行
選擇對應的server來運行項目
6.運行的頁面
http://localhost:8080/Struts2/index.html
http://localhost:8080/Struts2/example/Welcome.jsp頁面
點擊“Sign On”以后
7.新增文件
修改struts.xml文件,增加
<action name="hello2"><result>/hello.jsp</result></action>
在WebContent下新建hello.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!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>
HelloWorld! By http://blog.csdn.net/unix21
</body>
</html>
重新Run文件,使用http://localhost:8080/Struts2/hello2就可以訪問
至此一個基本的struts的helloworld項目就完成了。
更多參考:在Eclipse中配置Struts2項目(二)??? 很老的文章,不過每一步很詳細,可以看看特別是JDK和Tomcat的配置
總結
以上是生活随笔為你收集整理的使用eclipse创建Struts2项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java开发环境的搭建以及使用eclip
- 下一篇: 家庭局域网开启AP隔离利用无线路由器互连