Struts 2.5.20 在Eclipse IDE中的配置和開發實例



零、參考博客
1、Struts框架入門教程
2、Struts 2.5.10.1配置

3、eclipse中搭建Struts2.5.16

4、Struts2.5+eclipse+tomcat8.5配置


注意: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 已經不存在了,需要修改為: org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter


一、創建web工程
老套路,如下所示:

二、下載Struts 2.5.20 jar 

1、https://struts.apache.org/download.cgi#struts2520

2、從struts-2.5.20-all\struts-2.5.20\lib 中找到下面幾個jar,添加到web工程的lib中,(不要將.jar包全部都添加進去,都添進去反而報錯)

附注:與Struts2.3比起來少了一個xwork-core-2.3.34.jar,多了一個log4j-api-2.7.jar。這是因為xwork-core-2.3.34.jar已經整合到Struts2-core中了,如果沒有導入log4j-api-2.7jar的話,在web.xml和struts.xml都配置正確的情況下,會報出以下的錯誤:

 

三、新建 action類:HelloWorldAction.java

package com.ews.cn;
 
public class HelloWorldAction {
 
    private String name;
 
    public String execute() throws Exception {
 
        System.out.println("getName:" + getName());
 
        if (getName().equals("") || getName() == null) {
            return "error";
        } else {
            return "success";
        }
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

  四、新建 struts.xml

方法是:選中項目——右鍵——新建——其他,在搜索框內輸入xml,然后選擇:    (注意一定要將該xml文件命名為:struts.xml,不能出錯。)

但是在新生成的xml文檔中只有版本信息,因此還需要將以下內容復制到xml文檔中。

 

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">

這樣eclipse才會出現提示。以上需要復制的內容可以從Struts目錄下打開apps文件夾,其中有兩個war包,隨便其中一個war包解壓出來,依次打開WEB-INF——src找到里面的Struts.xml就可以找到上面的這段話。

 

1、查看 struts-2.5.20\apps\WEB-INF\classes 里找到 struts.xml,為了讓其在tomact的 classes中生成,必須放倒工程的src下面,不然找不到action。

2、對struts.xml進行修改:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="true" />

<package name="com.ews.cn" extends="struts-default">
<!-- <global-allowed-methods>add, update</global-allowed-methods> -->
<action name="hello" class="com.ews.cn.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="error">/Error.jsp</result>
</action>
</package>
</struts>

  

五、修改 web.xml,配置 struts 過濾器

<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>HelloWorldStruts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app> 

注意:

struts2.5 中的是 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter , 而不是 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

六、分別創建三個頁面 

1、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<h1>Hello World Struts2</h1>
<form action="hello">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/>
<input type="submit" value="Enter"/>
</form>
</body>
</html>

  

2、HelloWorld.jsp

<s:property value="name"/> 需要引入標簽 <%@ taglib uri="/struts-tags" prefix="s"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
Hello World, Welcome! <s:property value="name"/>
</body>
</html>

  

3、Error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
You did not have entered a name!
</body>
</html>

  

七、將Web應用部署到Apache Tomcat服務器上

 

八、測試 

訪問:http://localhost:8081/MyStruts2/index.jsp

1、正常輸入name

2、name為空


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM