struts2的第一個小頁面

 

超簡單的頁面,就是一個歡迎頁面,對於老手來講,能夠飄過去了。html

剛接觸struts2,就一個歡迎頁面就沒有整明白。java

出現了錯誤問題。先是在Ubuntu上本身寫,不借助IDE,結果將所須要的文件都放在指定的目錄下,結果不能出來效果。而後就下載了myeclipse,藉助IDE,結果仍是調試不經過。最後憤而轉Win。web

一、jdk,tomcat安裝好後,測試tomcat成功。apache

http://localhost:8080/編程

出現Apache頁面瀏覽器

二、下載struts2tomcat

下載的版本是struts-2.3.16.1,app

而後將apps下的struts2-blank放到tomcat的webapps下框架

瀏覽器輸入http://localhost:8080/struts2-blankeclipse

而後就能夠看到效果。也就是說其實struts2是不須要配置的相似環境變量的,因爲出現不少問題,我就覺得struts2還須要什麼配置環境變量之類的。

如今也就理解了struts2更應該說是一個編程模型,而不是一個tomcat,myeclipse這樣的工具,也就存在什麼配置環境變量了。

三、在myeclipse的項目中導入struts2的jar包,聽java培訓機構李剛老師的視頻說,不要一股腦的將struts2的lib下的全部的jar包都導入,會有問題。我也不知道之前出現的問題是否是跟這個有關。將struts2-blank\lib下的jar包導入到項目的路徑中。具體步驟,右擊項目->Build Path->Add libraries,選擇user library,next,或者新建或者使用已經建好的,若是第一次配置就須要新建了,點擊user library,而後選擇new ,而後選擇struts2-blank\lib下面的包。這樣至少接下來的編譯是沒問題了

四、製做一個頁面,訪問時出現「hello world」字樣(學每一種語言都喜歡用這個實例,本身也用用)

a、寫一個index頁面

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=/struts2HelloWorld/HelloReader.action">
</head>

<body>
<h2>Loading ...</h2>
</body>
</html>
META選項是自動跳轉的意思。url請準確書寫,
因爲寫錯了url,一直出現資源不可用

b、因爲是使用了strtus2框架,咱們天然是但願攔截到這個訪問請求,

由此配置web.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_3_0.dtd">
<web-app>

<display-name>Struts Blank</display-name>
<filter>
<!-- 添加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>

</web-app>
注意版本號,儘管我還不知道版本到底有什麼,對程序有什麼影響,也不知道之前的錯誤是否跟版本號有關,最好改一下。
最後可能出現java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter這個錯誤,那麼咱們就須要將struts2-blank\lib下的jar包手工拷貝到應用名\WEB-INF\lib文件夾下面。

還有可能出現WARN No appenders could be found for logger (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory).這樣相似的警告,我把它忽略了。

c、攔截到請求以後,就須要配置struts.xml文件來定義action了

<span style="font-size:18px;"><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<package name="tutorial" extends="struts-default">
<action name="HelloReader" class="tutorial.HelloReader">
<result>
/helloworld.jsp
</result>
</action>
</package>

</struts>
</span>

注意修改struts.xml的版本號,版本號就是下載的struts2文件夾名後面的兩個數字,個人是struts-2.3.16.1,那麼改成2.3
其次出現的問題是提示我"struts-default",直接忽略,由於它是黃色歎號,這個警告有時有有時沒有,不知道緣由,或許是版本號吧(瞎猜的)

d、定義action處理類,


package tutorial;


import com.opensymphony.xwork2.ActionSupport;


public class HelloReader extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

public static final String MESSAGE = "Hello Reader ! I'm from struts2!";

public String execute() throws Exception{

setMessage(MESSAGE);
return SUCCESS;
}

public String getMessage() {
return message;
}


public void setMessage(String message) {
this.message = message;
}


//惟一的屬性,將被自動賦值並打印
private String message = "";

}
注意定義包名,使用默認包名,在action的class屬性寫的時候不知道怎麼寫,直接寫類名會出錯,我直接寫了一個包名
e、定義跳轉頁面

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%@ taglib prefix="s" uri="/struts-tags"%><html> <head> <title>Hello World</title> </head> <body> <!-- 使用property標籤來獲取action中的屬性,直接顯示在頁面中--> <h2> <s:property value="message"/> </h2> </body></html></span>這個也有可能出現警告,說/struts-tags這個存在,也是有時有,有時沒有,具體狀況未知。由於配置很差這些文件,也無法繼續學下去,記錄一下,也幫助一下其餘人,隨着深刻的學習,上面的問題可能會一一解釋清楚

相關文章
相關標籤/搜索