隨筆-34  評(píng)論-1965  文章-0  trackbacks-0

          有Struts 1.x經(jīng)驗(yàn)的朋友都知道Action是Struts的核心內(nèi)容,當(dāng)然Struts 2.0也不例外。不過(guò),Struts 1.x與Struts 2.0的Action模型很大的區(qū)別。

          ? Struts 1.x Stuts 2.0
          接口 必須繼承org.apache.struts.action.Action或者其子類 無(wú)須繼承任何類型或?qū)崿F(xiàn)任何接口
          表單數(shù)據(jù) 表單數(shù)據(jù)封裝在FormBean中 表單數(shù)據(jù)包含在Action中,通過(guò)Getter和Setter獲取

          雖然,理論上Struts 2.0的Action無(wú)須實(shí)現(xiàn)任何接口或繼承任何類型,但是,我們?yōu)榱朔奖銓?shí)現(xiàn)Action,大多數(shù)情況下都會(huì)繼承com.opensymphony.xwork2.ActionSupport類,并重載(Override)此類里的String execute()方法。具體的實(shí)現(xiàn),如例1所示:

          <% @ page contentType = " text/html; charset=UTF-8 " %>
          <% @ taglib prefix = " s " uri = " /struts-tags " %>
          < html >
          < head >
          ? ?
          < title > Hello World! </ title >
          </ head >
          < body >
          ? ?
          < h2 >< s:property value ="message" /></ h2 >
          </ body >
          </ html >
          例1 HelloWorld.jsp

          package tutorial;

          import java.text.DateFormat;
          import java.util.Date;

          import com.opensymphony.xwork2.ActionSupport;

          public class HelloWorld extends ActionSupport {
          ? ?
          private String message;
          ? ?
          ? ?
          public String getMessage() {
          ? ? ? ?
          return message;
          ? ?}

          ? ?
          ? ?@Override?
          ? ?
          public String execute() {
          ? ? ? ?message
          = " Hello World, Now is " + DateFormat.getInstance().format( new Date());
          ? ? ? ?
          return SUCCESS;
          ? ?}

          }
          例1 classes/tutorial/HelloWorld.java

          < package name ="ActionDemo" extends ="struts-default" >
          ? ?
          < action name ="HelloWorld" class ="tutorial.HelloWorld" >
          ? ? ? ?
          < result > /HelloWorld.jsp </ result >
          ? ?
          </ action >
          </ package >
          例1 classes/struts.xml中HelloWorld Action的配置

          在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/HelloWorld.action,可以看到如圖1所示頁(yè)面。

          圖1 HelloWorld輸出頁(yè)面
          圖1 HelloWorld輸出頁(yè)面

          參考JavaDoc,可知ActionSupport類實(shí)現(xiàn)了接口:

          默認(rèn)情況下,當(dāng)請(qǐng)求HelloWorld.action發(fā)生時(shí),Struts運(yùn)行時(shí)(Runtime)根據(jù)struts.xml里的Action映射集(Mapping),實(shí)例化tutoiral.HelloWorld類,并調(diào)用其execute方法。當(dāng)然,我們可以通過(guò)以下兩種方法改變這種默認(rèn)調(diào)用。這個(gè)功能(Feature)有點(diǎn)類似Struts 1.x中的LookupDispathAction。

          1. 在classes/sturts.xml中新建Action,并指明其調(diào)用的方法;
          2. 訪問(wèn)Action時(shí),在Action名后加上“!xxx”(xxx為方法名)。

          實(shí)現(xiàn)方法請(qǐng)參考例2:

          在classes/tutorial/HelloWorld.java中加入以下方法:
          public String aliasAction() {
          ? ? message
          ="自定義Action調(diào)用方法";
          ? ?
          return SUCCESS;
          }
          例2 classes/tutorial/HelloWorld.java代碼片段

          實(shí)現(xiàn)方法一,在classes/sturts.xml中加入下面代碼:
          <action name="AliasHelloWorld" class="tutorial.HelloWorld" method="aliasAction">
          ? ?
          <result>/HelloWorld.jsp</result>
          </action>
          例2 classes/struts.xml中AlaisHelloWorld Action的配置

          實(shí)現(xiàn)方法二,使用http://localhost:8080/Struts2_Action/HelloWorld!aliasAction.action地址來(lái)訪問(wèn)HelloWorld Action。

          在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/AliasHelloWorld.actionhttp://localhost:8080/Struts2_Action/HelloWorld!aliasAction.action,可以看到如圖2所示頁(yè)面。

          圖2 自定義Action調(diào)用方法頁(yè)面
          圖2 自定義Action調(diào)用方法頁(yè)面

          通過(guò)上面的兩個(gè)例子,細(xì)心的朋友應(yīng)該可能會(huì)發(fā)現(xiàn)classes/tutorial/HelloWorld.java中Action方法(execute和aliasAction)返回都是SUCCESS。這個(gè)屬性變量我并沒(méi)有定義,所以大家應(yīng)該會(huì)猜到它在ActionSupport或其父類中定義。沒(méi)錯(cuò),SUCCESS在接口com.opensymphony.xwork2.Action中定義,另外同時(shí)定義的還有ERROR, INPUT, LOGIN, NONE

          此外,我在配置Action時(shí)都沒(méi)有為result定義名字(name),所以它們默認(rèn)都為success。值得一提的是Struts 2.0中的result不僅僅是Struts 1.x中forward的別名,它可以實(shí)現(xiàn)除forward外的很激動(dòng)人心的功能,如將Action輸出到FreeMaker模板、Velocity模板、JasperReports和使用XSL轉(zhuǎn)換等。這些都過(guò)result里的type(類型)屬性(Attribute)定義的。另外,您還可以自定義result類型。

          下面讓我們來(lái)做一個(gè)Velocity模板輸出的例子,首先在classes/struts.xml中新建一個(gè)Action映射(Mapping),將其result類型設(shè)為velocity,如以下代碼所示:
          <action name="VMHelloWorld" class="tutorial.HelloWorld">
          ? ?
          <result type="velocity">/HelloWorld.vm</result>
          </action>
          例3 classes/struts.xml中VMHelloWorld Action的配置

          新建HelloWorld.vm,內(nèi)容如下所示:
          <html>
          ?
          <head>
          ? ?
          <title>Velocity</title>
          ? ?
          <meta http-equiv="content-type" content="text/html; charset=UTF-8">
          ?
          </head>
          ?
          <body>
          ? ?
          <h2>Message rendered in Velocity: $message</h2>
          ?
          </body>
          </html>
          例3 HelloWorld.vm

          在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/VMHelloWorld.action,頁(yè)面輸出如下圖3所示。

          圖3 HelloWorld.vm的輸出頁(yè)面
          圖3 HelloWorld.vm的輸出頁(yè)面
          要運(yùn)行例3需要在WEB-INF/lib中添加以下幾個(gè)包:

          前面,我花了不少的時(shí)間討論Action的輸出。我老板有句名言——程序無(wú)非就是輸入、操作和輸出。因此,現(xiàn)在我們要討論一下輸入——表單輸入。

          使用Struts 2.0,表單數(shù)據(jù)的輸入將變得非常方便,和普通的POJO一樣在Action編寫(xiě)Getter和Setter,然后在JSP的UI標(biāo)志的name與其對(duì)應(yīng),在提交表單到Action時(shí),我們就可以取得其值。

          讓我們看一個(gè)例子,新建Login Action,它通過(guò)Login.jsp的表單獲得用戶名和密碼,驗(yàn)查用戶名是否為“max”,密碼是否則為“secret”。如果,兩者都符合,就在HelloWorld中顯示“Welcome, max”,否則顯示“Invalid user or Password”。

          package tutorial;

          import com.opensymphony.xwork2.ActionSupport;

          publicclass Login extends ActionSupport {
          ? ?
          private String name;
          ? ?
          private String password;
          ? ?
          private String message;
          ? ?
          ? ?
          public String getName() {
          ? ? ? ?
          return name;
          ? ?}

          ? ?
          ? ?
          publicvoid setName(String name) {
          ? ? ? ?
          this.name = name;
          ? ?}

          ? ?
          ? ?
          public String getPassword() {
          ? ? ? ?
          return password;
          ? ?}

          ? ?
          ? ?
          publicvoid setPassword(String password) {
          ? ? ? ?
          this.password = password;
          ? ?}

          ? ?
          ? ?
          public String getMessage() {
          ? ? ? ?
          return message;
          ? ?}


          ? ?@Override
          ? ?
          public String execute() {
          ? ? ? ?
          if("max".equals(name) &&"Secret".equals(password)) {
          ? ? ? ? ? ?message
          ="Welcome, "+ name;
          ? ? ? ?}
          else{
          ? ? ? ? ? ?message
          ="Invalid user or password";
          ? ? ? ?}

          ? ? ? ?
          return SUCCESS;
          ? ?}

          }
          例4 classes/tutorial/Login.java

          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags"%>
          <html>
          <head>
          ? ?
          <title>Login</title>
          </head>
          <body>
          <s:form action="Login" method="POST">
          ? ?
          <s:textfield name="name" label="User name"/>
          ? ?
          <s:password name="password" label="Password"/>
          ? ?
          <s:submit value="Submit"/>
          </s:form>
          </body>
          </html>
          例4 Login.jsp

          <action name="Login" class="tutorial.Login">
          ? ?
          <result>/HelloWorld.jsp</result>
          </action>
          例4 classes/struts.xml中Login Action的配置

          運(yùn)行Tomcat,在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/Login.jsp,出現(xiàn)如圖4所示頁(yè)面。

          圖4 Login.jsp輸出頁(yè)面
          圖4 Login.jsp輸出頁(yè)面

          分別在User name中輸入“max”和“secret”,點(diǎn)擊“Submit”按鈕,出現(xiàn)如圖5所示頁(yè)面。

          圖5 Login成功頁(yè)面
          圖5 Login成功頁(yè)面

          在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/Login.jsp,分別在User name中輸入“Scott”和“password”,點(diǎn)擊“Submit”按鈕,出現(xiàn)如圖6所示頁(yè)面。

          圖6 Login失敗頁(yè)面
          圖6 Login失敗頁(yè)面?

          Struts 2.0更厲害的是支持更高級(jí)的POJO訪問(wèn),如user.getPassword()。我們可以用另一寫(xiě)法實(shí)現(xiàn)例4。首先,將name和password從Login類中分離出來(lái),到新建類User中。這樣對(duì)我們開(kāi)發(fā)多層系統(tǒng)尤其有用。它可以使系統(tǒng)結(jié)構(gòu)更清晰。

          package tutorial;

          import com.opensymphony.xwork2.ActionSupport;

          publicclass LoginX extends ActionSupport {
          ? ?
          private User user;
          ? ?
          private String message;
          ? ?
          ? ?
          publicvoid setUser(User user) {
          ? ? ? ?
          this.user = user;
          ? ?}

          ? ?
          ? ?
          public User getUser() {
          ? ? ? ?
          return user;
          ? ?}

          ? ?
          ? ?
          public String getMessage() {
          ? ? ? ?
          return message;
          ? ?}

          ? ?
          ? ?@Override
          ? ?
          public String execute() { ? ? ? ?
          ? ? ? ?
          if("max".equals(user.getName()) &&"secret".equals(user.getPassword())) {
          ? ? ? ? ? ?message
          ="Welcome, "+ user.getName();
          ? ? ? ?}
          else{
          ? ? ? ? ? ?message
          ="Invalid user or password";
          ? ? ? ?}

          ? ? ? ?
          return SUCCESS;
          ? ?}

          }
          例5 classes/tutorial/LoginX.java

          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags"%>
          <html>
          <head>
          ? ?
          <title>Login</title>
          </head>
          <body>
          <s:form action="LoginX" method="POST">
          ? ?
          <s:textfield name="user.name" label="User name"/>
          ? ?
          <s:password name="user.password" label="Password"/>
          ? ?
          <s:submit value="Submit"/>
          </s:form>
          </body>
          </html>
          例5 LoginX.jsp

          <action name="LoginX" class="tutorial.LoginX">
          ? ?
          <result>/HelloWorld.jsp</result>
          </action>
          例5 classes/struts.xml中的LoginX Action配置?

          很多時(shí)候我的同事會(huì)問(wèn)我:“如果我要取得Servlet API中的一些對(duì)象,如request、response或session等,應(yīng)該怎么做?這里的execute不像Struts 1.x的那樣在參數(shù)中引入。”開(kāi)發(fā)Web應(yīng)用程序當(dāng)然免不了跟這些對(duì)象打交道。在Strutx 2.0你可以有兩種方式獲得這些對(duì)象:非IoC(控制反轉(zhuǎn)Inversion of Control)方式和IoC方式。

          1. 非IoC方式

            要獲得上述對(duì)象,關(guān)鍵Struts 2.0中com.opensymphony.xwork2.ActionContext類。我們可以通過(guò)它的靜態(tài)方法getContext()獲取當(dāng)前Action的上下文對(duì)象。 另外,org.apache.struts2.ServletActionContext作為輔助類(Helper Class),可以幫助您快捷地獲得這幾個(gè)對(duì)象。

            • HttpServletRequest request = ServletActionContext.getRequest();
            • HttpServletResponse response = ServletActionContext.getResponse();
            • HttpSession session = request.getSession();

            如果你只是想訪問(wèn)session的屬性(Attribute),你也可以通過(guò)ActionContext.getContext().getSession()獲取或添加session范圍(Scoped)的對(duì)象。

          2. IoC方式

            要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個(gè)對(duì)象的意愿,通過(guò)實(shí)現(xiàn)相應(yīng)的接口做到這點(diǎn)。具體實(shí)現(xiàn),請(qǐng)參考例6 IocServlet.java。
          package tutorial;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpSession;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.ActionContext;
          import com.opensymphony.xwork2.ActionSupport;

          publicclass NonIoCServlet extends ActionSupport {
          ? ?
          private String message;
          ? ?
          ? ?
          public String getMessage() {
          ? ? ? ?
          return message; ? ? ? ?
          ? ?}

          ? ?
          ? ?@Override
          ? ?
          public String execute() { ? ?
          ? ? ? ?ActionContext.getContext().getSession().put(
          "msg", "Hello World from Session!");
          ? ? ? ?
          ? ? ? ?HttpServletRequest request
          = ServletActionContext.getRequest();
          ? ? ? ?HttpServletResponse response
          = ServletActionContext.getResponse(); ? ? ? ?
          ? ? ? ?HttpSession session
          = request.getSession();
          ? ? ? ?
          ? ? ? ?StringBuffer sb
          =new StringBuffer("Message from request: ");
          ? ? ? ?sb.append(request.getParameter(
          "msg"));
          ? ? ? ?sb.append(
          "<br>Response Buffer Size: ");
          ? ? ? ?sb.append(response.getBufferSize());
          ? ? ? ?sb.append(
          "<br>Session ID: ");
          ? ? ? ?sb.append(session.getId());
          ? ? ? ?
          ? ? ? ?message
          = sb.toString();
          ? ? ? ?
          return SUCCESS;
          ? ?}

          }
          例6 classes/tutorial/NonIoCServlet.java

          package tutorial;

          import java.util.Map;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpSession;

          import org.apache.struts2.interceptor.ServletRequestAware;
          import org.apache.struts2.interceptor.ServletResponseAware;
          import org.apache.struts2.interceptor.SessionAware;

          import com.opensymphony.xwork2.ActionContext;
          import com.opensymphony.xwork2.ActionSupport;

          publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
          ? ?
          private String message;
          ? ?
          private Map att;
          ? ?
          private HttpServletRequest request;
          ? ?
          private HttpServletResponse response; ? ?
          ? ?
          ? ?
          public String getMessage() {
          ? ? ? ?
          return message; ? ? ? ?
          ? ?}

          ? ?
          ? ?
          publicvoid setSession(Map att) {
          ? ? ? ?
          this.att = att;
          ? ?}

          ? ?
          ? ?
          publicvoid setServletRequest(HttpServletRequest request) {
          ? ? ? ?
          this.request = request;
          ? ?}

          ? ?
          ? ?
          publicvoid setServletResponse(HttpServletResponse response) {
          ? ? ? ?
          this.response = response;
          ? ?}

          ? ?
          ? ?@Override
          ? ?
          public String execute() { ? ? ? ?
          ? ? ? ?att.put(
          "msg", "Hello World from Session!");
          ? ? ? ?
          ? ? ? ?HttpSession session
          = request.getSession();
          ? ? ? ?
          ? ? ? ?StringBuffer sb
          =new StringBuffer("Message from request: ");
          ? ? ? ?sb.append(request.getParameter(
          "msg"));
          ? ? ? ?sb.append(
          "<br>Response Buffer Size: ");
          ? ? ? ?sb.append(response.getBufferSize());
          ? ? ? ?sb.append(
          "<br>Session ID: ");
          ? ? ? ?sb.append(session.getId());
          ? ? ? ?
          ? ? ? ?message
          = sb.toString();
          ? ? ? ?
          return SUCCESS;
          ? ?}

          }
          例6 classes/tutorial/IoCServlet.java

          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags"%>
          <html>
          <head>
          ? ?
          <title>Hello World!</title>
          </head>
          <body>
          ? ?
          <h2>
          ? ? ? ?
          <s:property value="message" escape="false"/>
          ? ? ? ?
          <br>Message from session: <s:property value="#session.msg"/>
          ? ?
          </h2>
          </body>
          </html>
          例6 Servlet.jsp

          <action name="NonIoCServlet" class="tutorial.NonIoCServlet">
          ? ?
          <result>/Servlet.jsp</result>
          </action>
          <action name="IoCServlet" class="tutorial.IoCServlet">
          ? ?
          <result>/Servlet.jsp</result>
          </action>
          例6 classes/struts.xml中NonIocServlet和IoCServlet Action的配置

          運(yùn)行Tomcat,在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/NonIoCServlet.action?msg=Hello%20World!http://localhost:8080/Struts2_Action/IoCServlet.action?msg=Hello%20World!,出現(xiàn)如圖7所示頁(yè)面。


          圖7 Servlet.jsp的輸出頁(yè)面?
          圖7 Servlet.jsp的輸出頁(yè)面

          在Servlet.jsp中,我用了兩次property標(biāo)志,第一次將escape設(shè)為false為了在JSP中輸出<br>轉(zhuǎn)行,第二次的value中的OGNL為“#session.msg”,它的作用與session.getAttribute("msg")等同。
          關(guān)于property或其它標(biāo)志,可以參考我的上一篇文章《常用的Struts 2.0的標(biāo)志(Tag)介紹 》。
          posted on 2006-10-25 12:10 Max 閱讀(99541) 評(píng)論(118)  編輯  收藏 所屬分類: Struts 2.0系列
          評(píng)論共2頁(yè): 1 2 下一頁(yè) 

          評(píng)論:
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-03-27 16:22 | Michael
          我用自己定義的方法怎么沒(méi)能把值傳到JSP上?用execute這個(gè)方法就行.
          我的方法(在action中)
          public String show()
          {
          return SUCCESS;
          }
          struts.xml中配置如下.
          <action name="ActionShow" class="tutorial.ActionShow" method="show">
          <result name="ok" type="redirect">showAll.jsp</result>
          </action>
          我的action繼承ActionSupport這個(gè)類  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-03-27 23:12 | Max
          @Michael
          原因你把result的類型(type)設(shè)為了"redirect", 而且名稱(name)也不對(duì)。如果Action方法返回SUCCESS時(shí),name應(yīng)為success。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-03-27 23:14 | Max
          @jintian
          對(duì)不起,我忘記把User類的代碼貼上去了,其實(shí)就和Seed說(shuō)的一樣。
          @seed
          謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-04 15:00 | panka
          好文章,看了很有收獲,繼續(xù)看下去  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-04-10 19:54 | yang
          請(qǐng)問(wèn)一下:
          1、<s:property value="message" escape="false"/>
          如果有多個(gè)Action,那怎么知道這個(gè)message是哪個(gè)Action里的?

          2、表達(dá)式的問(wèn)題,有${表達(dá)式}、帶有#的表達(dá)式(如上文中的#session.msg),還有%開(kāi)始的表達(dá)式,這些好像都是取出里面的值,請(qǐng)問(wèn)這些$、#、%開(kāi)頭的表達(dá)式有什么不同?各自在什么情況下使用?

          3、IoC方式不是要注入嗎?那HttpServletRequest request;HttpServletResponse response這兩個(gè)怎么注入實(shí)例呢?不用寫(xiě)關(guān)聯(lián)配置嗎?

          可以幫我解惑一下嗎?謝謝!!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-12 23:49 | henry
          我是struts新手,struts 1.*也沒(méi)怎么接觸過(guò),打算直接學(xué)struts 2.0,希望不會(huì)有太大困難,看了你的文章得到很大幫助,感謝你的辛勤工作,寫(xiě)出這么好的介紹文章,希望能看到更多關(guān)于介紹struts 2.0的文章,^_^  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-13 09:42 | Max
          @yang
          1、你的“多個(gè)Action”是什么意思?

          2、OGNL中的${...}可以在定義Action的result時(shí),傳遞變量,如/deleteBook.action?isbn=${isbn}。也可以在國(guó)際化時(shí)引用變量;
          #用于在頁(yè)面上引用ActionContext的值,也可以構(gòu)造映射(Map)等,如#request.xxx;
          %{...}在標(biāo)簽屬性中引用變量,如<s:url value="Edit.action?isbn=%{isbn}" />

          3、IoC也有人叫DI(依賴注入),HttpServletRequest request和HttpServletResponse response可以通過(guò)攔截器注入,詳情請(qǐng)參考《Struts 2的基石——攔截器(Interceptor)》。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-13 17:12 | yskey
          終于找到價(jià)值的struts2的資料了,謝謝樓主,呵呵,希望能夠給出更多的關(guān)于這個(gè)主題的內(nèi)容,謝謝,呵呵!!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-16 14:10 | hermit
          樓主你好,目前正在struts2的學(xué)習(xí)中,遇到了一個(gè)問(wèn)題,希望有時(shí)間指導(dǎo)一下。
          我在action中把值set到變量里了(比如,user.setPassword("dddd")),但是在頁(yè)面為獲得我想要的值<s:textfield name="user.password" value="%{user.password}"></s:textfield>
          不明白為什么?請(qǐng)高手指點(diǎn)!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-16 22:40 | Max
          @hermit
          我不是很明白你什么問(wèn)題,能否詳細(xì)一點(diǎn)?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-17 09:40 | hermit
          Max ,我昨天問(wèn)的問(wèn)題已經(jīng)解決了,晚上自習(xí)看了一下,是我的屬性名沒(méi)有對(duì)應(yīng)上,其實(shí)我是做一個(gè)修改頁(yè)面,獲得數(shù)據(jù)庫(kù)信息傳到頁(yè)面上,頁(yè)面form對(duì)應(yīng)的就是user中的屬性,由于我的password屬性,和user類中不同名,所以信息沒(méi)有傳到頁(yè)面。但是以前1.X的版本這種情況就會(huì)報(bào)錯(cuò)了,2.0沒(méi)有報(bào)錯(cuò)所以沒(méi)有發(fā)現(xiàn)。呵呵,謝謝樓主關(guān)注,也非常感謝你提供了這么多Structs2的相關(guān)介紹,幫助很大。繼續(xù)關(guān)注你的blog,希望還可以看到更多更好的文章。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-23 10:27 | javaChicken
          我想得到cookies怎么辦  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-24 14:18 | ddd
          請(qǐng)問(wèn), web.xml設(shè)置時(shí),如何添加filter, 對(duì)encoding進(jìn)行制定。。

          是否跟以前一樣? 自己做一個(gè)filter, 然后讀取web.xml的設(shè)置?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-25 09:23 | Max
          @javaChicken
          先在Action中拿到HttpServletRequest的對(duì)象(請(qǐng)參考《Struts 2.0的Action講解》),然后調(diào)用request.getCookies()。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-25 09:25 | Max
          @ddd
          我認(rèn)為不必如此麻煩,關(guān)鍵找出亂碼的原因。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-25 11:13 | do
          請(qǐng)問(wèn):我在那個(gè)IoCServlet里面沒(méi)有看到session.setAttribute("msg","Hello World from Session!");,是怎么把msg放到session里面的?
          這個(gè)Map att是起什么作用?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-25 13:30 | ddd
          @Max
          亂碼出現(xiàn)的地方:

          在頁(yè)面上輸入的時(shí)候, 還不亂碼,進(jìn)入action(java類)后, 就亂了。。

          通過(guò)debug看。。。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-25 13:46 | ddd
          @do

          因?yàn)樗鳛镾etter setSession的變量, 是會(huì)被自動(dòng)運(yùn)行的。。

          Setter是IoC(DI)的必須

            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-28 14:57 | mm
          在struts.xml中,兩個(gè)package中的action如何通信?

          <package name="user" extends="struts-default">
          <action name="login" class="userAction" method="login">
          <result type ="chain">mser</result>
          </action>
          </package>
          <package name="muser" extends="struts-default">
          <action name="mser" class="muserAction" method="login">
          <result>hello.jsp</result>
          </action>
          </package>

          login.action在user包中,mser.action在muser包中,像以上的調(diào)用,會(huì)不會(huì)出錯(cuò)呢?login.action可以連接到mser.action?請(qǐng)高手指教  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-04-28 15:10 | 小楊
          請(qǐng)教樓主:

          實(shí)現(xiàn)方法二,使用http://localhost:8080/Struts2_Action/HelloWorld!aliasAction.action地址來(lái)訪問(wèn)HelloWorld Action

          這個(gè)辦法我不行嘛,我用的是get方式提交。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-10 17:05 | jiajia
          struts2怎樣控制Action實(shí)例在session還是在request里面。struts1是在struts-config里面配置action時(shí),指定scope。struts2怎樣控制?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-11 09:26 | Max
          @jiajia
          你的“Action實(shí)例”是指什么?我的理解是:在Struts 1.x中Action是Singleton的,在Struts-config配置是form bean的scope。

          在Struts 2 中可以使用setAttribute的方法控制對(duì)象的scope。
            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-11 10:20 | jiajia
          @Max
          你的理解是對(duì)的,一般要往request或session里面放東西是會(huì)用到setAttribute方法,如果要Action實(shí)例在session里面,用你說(shuō)的setAttribute方法怎么控制對(duì)象的scope,能給個(gè)例子嗎?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-18 08:54 | 秋雨
          好呀,今后跟您混了.

          太詳細(xì)了.好......................  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-23 22:28 | 想飛就飛
          請(qǐng)問(wèn)樓主,我的問(wèn)題與jiajia類似
          在struts2中,我可以不調(diào)用request.setAttribute(xx,xxx)
          比如說(shuō)
          publicclass LoginX extends ActionSupport {
          private String message;

          public String getMessage() {
          return message;
          }

          public String execute() {
          message ="Welcome, "+ user.getName();
          return SUCCESS;
          }
          }
          頁(yè)面就可以直接獲取${message},請(qǐng)問(wèn)這默認(rèn)是request級(jí)別的嗎?
          如果是session級(jí)別的,是不是要在取得session后
          在代碼中明確寫(xiě)入,session.setAttribute(xx.xxxx)


            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-24 10:26 | Max
          @想飛就飛
          這些值是放在ActionContext中的,所以不是request或session等。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-25 03:33 | xiaohong
          我是JAVAEE的初學(xué)者,剛剛接觸struts,在上面的login的例子中,struts2 的功能是不是取代了jsp+servlet+javabean+jdbc中前兩個(gè)的功能,使jsp+servlet這兩種東西開(kāi)發(fā)和維護(hù)起來(lái)更方便,并且把對(duì)jsp和servlet的配置從以前的web.xml中分離出來(lái),轉(zhuǎn)換成了action放在struts.xml中,至于從數(shù)據(jù)庫(kù)中獲得數(shù)據(jù)還得用以前的 PreparedStatement 結(jié)合javabean的方法來(lái)實(shí)現(xiàn)!


          我這種理解對(duì)嗎!
          在線等回復(fù)!!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-25 09:52 | Max
          @xiaohong
          1、在JSP中,無(wú)論是jsp或struts或者其它框架,其最終起作用的都是Servlet;
          2、訪問(wèn)數(shù)據(jù)庫(kù)是需要通過(guò)JDBC,但是由于直接使用JDBC過(guò)于繁鎖,所以現(xiàn)在有很ORM框架幫助開(kāi)發(fā)者訪問(wèn)數(shù)據(jù)庫(kù),如iBatis、hibernate、JDO和Toplink等。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-05-31 17:46 | wangchb
          按照您的提點(diǎn)我寫(xiě)了一個(gè)test

          在實(shí)現(xiàn)執(zhí)行非默認(rèn) action的時(shí)候出錯(cuò)了,希望能指點(diǎn)下!
          表單頁(yè)面
          <form action="helloDemo/Hello!myFunction.action" method="post">

          user: <input type="text" name="user"><p>
          pass:<input type="text" name="pass"><p>
          <input type="submit" value="提交">
          </form>

          struts.xml

          <package name ="helloDemo" extends ="struts-default" namespace="/helloDemo">

          <action name ="Hello" class ="demo.Hello" >

          <result name="render" type="freemarker">/${template}</result> <!-- use the template to render the data -->

          </action>

          </package>

          這個(gè)是您說(shuō)的第二種方法,結(jié)果報(bào)錯(cuò)說(shuō) 找不到action
          第一種方法執(zhí)行通過(guò)
          幫忙解決下 謝謝了,不勝感激  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-06-04 23:35 | Max
          @wangchb
          檢查一下你form實(shí)際提交路徑是否為http://hostname:port/helloDemo/Hello!myFunction.action。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-06-05 16:26 | domy
          初學(xué)者,感謝你的無(wú)私奉獻(xiàn)!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-06-21 16:04 | 不吃芹菜
          struts1.2時(shí)有一個(gè)forward的action,解決的是不用編寫(xiě)action類達(dá)到跳轉(zhuǎn)頁(yè)面的目的,那么struts2應(yīng)該怎么做呢?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-06-29 11:40 | eric
          如何實(shí)現(xiàn) struts2 + velocity 1.5 的配置?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-07-05 16:50 | Michael
          2 wangchb:
          檢查一下struts.xml里有沒(méi)有
          <constant name="struts.enable.DynamicMethodInvocation" value="false" />
          把false改成true  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-09 13:45 | yiwuyun
          怎么VMHelloWorld用不起啊 ,咋會(huì)說(shuō)找不到模板喃HelloWorld.vm。其他兩個(gè)都用得起
          我完全按照樓主的說(shuō)法做的啊,也下了4個(gè)相應(yīng)的包,只
          不過(guò)有些版本比較高而已,郁悶。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-09 15:12 | yiwuyun
          我想樓主所說(shuō)的那4個(gè)包有點(diǎn)問(wèn)題,或者還不夠,總之我又糊里糊涂地下了許多包,把它拷到WEB-INF/lib下,這次竟然通過(guò)了,有意思,哈哈。當(dāng)然我弄不清楚哪些是必須的,哪些不是,郁悶。有時(shí)下多了不行,有時(shí)下少了也不行。哎郁悶死了。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-09 23:53 | Max
          @yiwuyun
          Sorry for that  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-10 19:37 | 冰紅茶
          實(shí)現(xiàn)方法二,使用http://localhost:8080/Struts2_Action/HelloWorld!aliasAction.action地址來(lái)訪問(wèn)HelloWorld Action。

          上面方法不行 MAX  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-10 19:44 | 冰紅茶
          這個(gè)可以
          在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Action/AliasHelloWorld.action

          下面這個(gè)不行
          使用http://localhost:8080/Struts2_Action/HelloWorld!aliasAction.action地址來(lái)訪問(wèn)HelloWorld Action  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-20 09:48 | wyg405
          非常好的文章!受宜  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-24 17:42 | renminyan
          實(shí)例 HelloWorld.vm少了commons-digester-1.8.jar包了
          但是我還是不知道.vm文件的好處,對(duì)這個(gè)并不了解。
          謝謝樓主的文章~~~~~~  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-25 18:42 | 明人
          "雖然,理論上Struts 2.0的Action無(wú)須實(shí)現(xiàn)任何接口或繼承任何類型,但是,我們?yōu)榱朔奖銓?shí)現(xiàn)Action,大多數(shù)情況下都會(huì)繼承com.opensymphony.xwork2.ActionSupport類,并重載(Override)此類里的String execute()方法。"

          max兄,重載是overload,重寫(xiě)是override,兩者的區(qū)別不用多說(shuō),接觸java的朋友自然明白。

          我想這多半是max兄的筆誤,但還是指出,以免一些朋友產(chǎn)生誤會(huì),畢竟您的struts2系列文章影響很大,好多人都是看著您的文章進(jìn)入struts2的世界的,所以嚴(yán)格律己是必要的,這樣才能更好的造福人群。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-07-26 20:03 | 小小
          發(fā)給我點(diǎn)struct2.0的學(xué)習(xí)資料吧,,謝謝啦,,
          pxcong007@126.com  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-28 21:02 | yiwuyun
          @明人
          哈哈max兄沒(méi)有筆誤,你去看看java自動(dòng)生成的源文件就知道了。它就是@override.  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-07-31 09:55 | lf
          例3中我導(dǎo)入以下幾個(gè)包,但是不成功。
          avalon-logkit-2.2.1.jar
          commons-collections-3.2.jar
          velocity-1.5.jar
          velocity-tools-view-1.3.jar

          頁(yè)面提示:
          type Exception report

          message

          description The server encountered an internal error () that prevented it from fulfilling this request.

          exception

          javax.servlet.ServletException: Filter execution threw an exception


          root cause

          java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
          org.apache.velocity.runtime.resource.ResourceManagerImpl.initialize(ResourceManagerImpl.java:165)
          org.apache.velocity.runtime.RuntimeInstance.initializeResourceManager(RuntimeInstance.java:594)
          org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:241)
          org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:534)
          org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:144)
          org.apache.struts2.views.velocity.VelocityManager.newVelocityEngine(VelocityManager.java:473)
          org.apache.struts2.views.velocity.VelocityManager.init(VelocityManager.java:243)
          org.apache.struts2.dispatcher.VelocityResult.doExecute(VelocityResult.java:130)
          org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
          com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
          com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
          com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
          com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
          com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
          。。。。
          請(qǐng)高手相告我錯(cuò)在哪了,謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-07-31 23:36 | Max
          @lf
          使用Struts 2.0.8GA版本,至少需要以下包:
          commons-logging-1.1.jar
          freemarker-2.3.8.jar
          ognl-2.6.11.jar
          readme.txt
          struts2-core-2.0.8.jar
          xwork-2.0.3.jar  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-08-01 08:46 | lf
          @Max
          你所說(shuō)的包除了readme.txt外,我都有了,我的問(wèn)題是實(shí)現(xiàn)例3時(shí)我所導(dǎo)入的包是否有錯(cuò)或者差了哪些包,具體問(wèn)題提示已在前2樓的發(fā)言中說(shuō)明了,請(qǐng)指教。
          另這個(gè)readme.txt是什么東東哦,能否一起告知,謝謝。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-08-01 17:02 | jht
          @lf
          你的問(wèn)題我知道,還需要導(dǎo)入
          common-lang
          common-collection
          common-digester
          的jar包
          avalon-logkit-2.2.1.jar 好像找不到下載的,不過(guò)我也沒(méi)有用到  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-08-03 11:44 | 小超
          請(qǐng)問(wèn)下,一個(gè)action執(zhí)行后怎么返回到令一個(gè)action中
          <action name="add" class="test.Add">
          <result name="success">/List.action</result>
          </action>
          <action name="List" class="test.List">
          <result name="success">list.jsp</result>
          </action>
          這樣頁(yè)面報(bào)錯(cuò),說(shuō)找不到List.action,我單獨(dú)訪問(wèn)List.action是沒(méi)有問(wèn)題的.   回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-08-03 21:32 | gavin
          我寫(xiě)的
          <s:form action="/sys/login!input.action" method="post" theme="simple">
          <table style="width:40%;" border="1">

          為什么生成以后就是
          <form id="login" onsubmit="return true;" action="/kangsoft/sys/login" method="post">而不是期望的
          <form id="login" onsubmit="return true;" action="/kangsoft/sys/login!input.action" method="post">呢
          請(qǐng)問(wèn)各位該怎么解決呢
          上面有人問(wèn)過(guò)但是沒(méi)人回答哦  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-08-05 11:28 | binger
          @jht
          avalon-logkit-2.2.1.jar

          我也找不到這個(gè)包的下載地址  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-08-05 11:30 | binger
          誰(shuí)有avalon-logkit-2.2.1.jar 這個(gè)包,傳一個(gè)吧,先謝謝了~~

          hncj_l.h.x@163.com

            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-08-07 14:01 | pxcong
          請(qǐng)問(wèn)MAX
          如何 避免 在未提交前就進(jìn)行驗(yàn)證器驗(yàn)證  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-08-17 11:36 | ArthurWang
          為什么我的Velocity模板輸出的例子,輸出報(bào)錯(cuò),上面所說(shuō)的包都已經(jīng)導(dǎo)入了
          錯(cuò)誤內(nèi)容如下,
          java.lang.NoClassDefFoundError: org/apache/velocity/context/Context
          java.lang.Class.getDeclaredMethods0(Native Method)
          java.lang.Class.privateGetDeclaredMethods(Class.java:2365)
          java.lang.Class.getDeclaredMethods(Class.java:1763)
          com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:103)
          com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:84)
          com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:82)
          com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:155)
          java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:269)
          java.util.concurrent.FutureTask.run(FutureTask.java:123)
          com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:81)
          com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:121)
          com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:452)
          com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:492)
          com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:491)
          是說(shuō)org/apache/velocity/context/Context這個(gè)類找不到,可導(dǎo)入的velocity-1.4.jar中明明有這個(gè)類。
          請(qǐng)Max大哥指教  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-08-30 11:15 | tf
          有誰(shuí)可以提供
          commons-collections-3.2.jar
          velocity-1.4.jar
          velocity-tools-view-1.2.jar
          avalon-logkit-2.1.jar
          這幾個(gè)包嗎?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-09-05 13:57 | william
          請(qǐng)問(wèn)怎么樣在框架頁(yè)面中實(shí)現(xiàn)國(guó)際化啊?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-09-08 19:10 |
          從頁(yè)面中提交User的數(shù)據(jù)到Action中 Action 像你所示例的一樣寫(xiě):
          public class HelloWorld extends ActionSupport {
          private String name;
          private User user;

          public User getUser()
          {
          return user;
          }
          public void setUser()
          {
          this.user = user;
          }

          public String getName() {
          return name;
          }

          public void setName(String name) {
          this.name = name;
          }

          public String execute() {
          name = "Hello, " + name + "!";
          return SUCCESS;
          }
          }

          但運(yùn)行到Action是報(bào)一個(gè)錯(cuò),錯(cuò)誤信息如下:
          2007-9-8 18:42:23 com.opensymphony.xwork2.util.InstantiatingNullHandler nullPropertyValue
          嚴(yán)重: Could not create and/or set value back on to object
          ognl.NoSuchPropertyException: tutorial.HelloWorld.user [java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class tutorial.HelloWorld with modifiers "private"]
          at ognl.OgnlRuntime.setFieldValue(OgnlRuntime.java:1125)
          at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:77)
          at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:132)
          at com.opensymphony.xwork2.util.OgnlValueStack$ObjectAccessor.setProperty(OgnlValueStack.java:68)
          at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1629)

          我在Action 中使用public修飾user對(duì)象就可以了!
          public User user;
          是不是必須使用public 修飾javabean 呢?而你的示例代碼中卻是使用private修飾的.
          后來(lái)我嘗試初始化user, 代碼修改為: private User user = new User();運(yùn)行沒(méi)問(wèn)題.
          為什么private User user;卻不行?為什么public User user; 和 private User user = new User();可以運(yùn)行通過(guò)?

          Max 大哥,請(qǐng)你解釋一下,表單數(shù)據(jù)如何被封裝為POJO的過(guò)程!?
          我的郵箱:yezichen2@163.com  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-09-13 23:13 | mOOnzhaO
          真是很好  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-10-03 00:14 | zyw358486
          辛苦了,老兄!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-10-18 23:02 | moonboat
          樓主辛苦,正在學(xué)習(xí)中......  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-10-23 13:11 | Mark
          # re: Struts 2.0的Action講解 2007-11-11 11:06 | skycliff
          樓主幸苦了,希望看到你更優(yōu)秀的文章  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2007-11-22 22:52 | 冬國(guó)
          我想問(wèn)一下,就是我想在
          velocity中使用Struts2.0的標(biāo)簽
          那我該怎樣來(lái)實(shí)現(xiàn)呢!
          如果有時(shí)間的話給點(diǎn)提示,謝謝~~!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-11-26 10:00 | feek
          你好。。。我的action無(wú)法傳遞兩個(gè)參數(shù)
          比如我要傳username,和password

          在另一頁(yè)顯示它們的值  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-11-26 11:12 | feek
          終于解決了。。。原來(lái)get和set都要有和變量名相同的部份
          如private pwd;
          那么就要setPwd;和getPwd而不能setPPPP
          真是太不可思議了
          以前沒(méi)有弄過(guò)struts現(xiàn)在直接學(xué)struts2.0
          看了max兄的教程學(xué)了不少東西。。
          謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-11-27 11:58 | java開(kāi)發(fā)者
          Avalon-Logkit 1.1 說(shuō)明以及jar包的下載地址:

          Logkit is a logging API for Java. It can be used directly or as a hidden implementation behind the logging abstractions in either Jakarta's Avalon-Framework or Commons-Logging.

          This release adds more rotation strategies and output targets.

          Avalon-Phoenix 4.0

          After nearly a year since its first alpha release, Phoenix is complete. You can view Phoenix as one of a number of things:

          * A Server for Servers
          * A Container for Unrestricted Components
          * A Micro-Kernel

          The idea is that other servers sit on top of Phoenix. These could be EJB, Mail (refer Jakarta JAMES), HTTP, etc.

          Phoenix has JMX capability via MX4J. Phoenix is configurable via XML, allowing replacement of its components.

          Applications for Phoenix are distributed in server archives (SAR files) Apps are laced together using XML allowing replacement of components post development.

          Background

          All Avalon projects honor some central designs and patterns:

          * Component Orientated Programming
          * Inversion of Control
          * Separation of Concerns
          * Interface and Implementation Separation
          * Minimalization of use of static.

          The Avalon team are continuing development of Logkit and Phoenix as well as other containers for different deployment needs and scenarios. The Avalon project also comprises several reusable jars for server side
          development as well as example applications for Phoenix.

          Links

          Logkit - http://jakarta.apache.org/avalon/logkit/" target="_new">http://jakarta.apache.org/avalon/logkit/
          Phoenix - http://jakarta.apache.org/avalon/phoenix/" target="_new">http://jakarta.apache.org/avalon/phoenix/

          Avalon project - http://jakarta.apache.org/avalon/
          Framework - http://jakarta.apache.org/avalon/framework/

          Avalon's patterns (a good starting point) - http://jakarta.apache.org/avalon/framework/guide-patterns-in-avalon.html
          Avalon-Framework's lifecycle interfaces (another good starting point) - http://jakarta.apache.org/avalon/framework/reference-the-lifecycle.html

          - The Avalon team.   回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2007-12-03 15:40 | 劉文濤
          我的那個(gè)IOC 和 非IOC 那個(gè) ,效果是出來(lái)了.但控制臺(tái) 打印 :
          --------------------------------------
          嚴(yán)重: ParametersInterceptor - [setParameters]: Unexpected Exception caught setti
          ng 'msg' on 'class demo.action.IoCServletNonAction: Error setting expression 'ms
          g' with value '[Ljava.lang.String;@57ae58'
          ----------------------------------------------
          不知道到 max 遇到這個(gè)問(wèn)題沒(méi)有.  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-01-08 15:47 | zyhxl66
          最近正在學(xué)習(xí)struts2.0,今天看了樓主的struts2.0資料總結(jié),幫助很大的!總結(jié)的挺細(xì)致!支持!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-01-11 14:34 | asd
          最近正在學(xué)習(xí)struts2.0,今天看了樓主的struts2.0資料總結(jié),幫助很大的!總結(jié)的挺細(xì)致!我的第一個(gè)事例也做完了,謝謝! 支持!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-01-11 14:44 |
          avalon-logkit-2.1.jar
          backport-util-concurrent-3.0.jar
          commons-collections-3.2.jar
          commons-collections.jar
          commons-dbcp-1.1.jar
          commons-digester-1.8.jar
          commons-fileupload-1.1.1.jar
          commons-io-1.1.jar
          commons-lang-2.2.jar
          commons-logging-1.1.jar
          commons-pool-1.1.jar
          dwr.jar
          freemarker-2.3.8.jar
          ibatis-2.3.0.677.jar
          msbase.jar
          mssqlserver.jar
          msutil.jar
          ognl-2.6.11.jar
          retrotranslator-runtime-1.2.0.jar
          retrotranslator-transformer-1.2.0.jar
          spring.jar
          struts2-core-j4-2.0.6.jar
          struts2-spring-plugin-j4-2.0.6.jar
          velocity-1.5.jar
          velocity-tools-view-2.0-beta1.jar
          xwork-j4-2.0.1.jar
          這是我的一個(gè)項(xiàng)目 struts2+spring+ibatis,和velocity以及要dwr的集合。
          我用了以上的包,基本目的能達(dá)到,但是有人說(shuō)我用的包太多,想請(qǐng)高手指教。我是一個(gè)剛剛學(xué)struts2的新手,第一次作開(kāi)發(fā),現(xiàn)謝謝!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-01-18 17:34 | llddy
          我想問(wèn)一下 關(guān)于 struts2訪問(wèn)地址的問(wèn)題
          http://localhost:8000/login/show.action
          他們?cè)L問(wèn)地址都是 *.action 的形式結(jié)尾.
          我現(xiàn)在想把*.action 改成 *.html 不知道怎么處理.
          因?yàn)? struts1.*都是修改web文件就可以
          struts2好像不可以我試過(guò) ....

          請(qǐng)您 -----幫我回復(fù)----- 一下 謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-02-22 17:30 | laibin
          感謝MAX!  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-03-08 20:01 | tomson
          it seems that there is no datepicker tag, just datetimepicker.  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-04-01 10:34 | 金昌
          兄弟 你太好了 學(xué)習(xí)ING...  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-04-11 10:01 | 111
          我想問(wèn)下。。。我在action中把個(gè)bean存入session中。。。為什么取不出來(lái)???
          存的時(shí)候是
          private Map session;
          session.put(名稱,bean);
          取的時(shí)候是
          this.bean = (強(qiáng)制轉(zhuǎn)換)session.get(名稱);

          請(qǐng)問(wèn)錯(cuò)在哪啊???  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-05-08 17:04 | JIAJIAJIAJIA
          我的user怎么點(diǎn)不出來(lái)getPassword().....
          高手指點(diǎn)下 謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-06-02 10:37 | 沉默不是金
          樓主,讀了你的很多篇struts2的介紹,感覺(jué)寫(xiě)得很容易看懂,昨天去書(shū)店看了本李剛寫(xiě)的介紹struts2的書(shū),洋洋灑灑寫(xiě)了估計(jì)有1000多頁(yè)吧,看得我云里霧里,現(xiàn)在的java方面的書(shū)啊,尤其是國(guó)內(nèi)出版的,根本沒(méi)自己的東西,網(wǎng)上copycopy,找國(guó)外的技術(shù)手冊(cè)翻譯翻譯,作者可能幾個(gè)月就能出本1000多頁(yè)的書(shū),勸大家買書(shū)時(shí)看仔細(xì)了,別浪費(fèi)銀子和時(shí)間。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-06-10 12:55 | jbas
          樓主,你好,
            請(qǐng)教一下,為什么我用
          HttpServletResponse response = ServletActionContext.getResponse();
          response.getWriter().println(str);

          不能輸出一段javascript或string字符串到目標(biāo)頁(yè)去,我以前用struts1是可以的,但現(xiàn)用struts2,不行了,而且如果我加了上面的代碼,展示的jsp頁(yè)面也會(huì)出現(xiàn)亂碼,把response相關(guān)代碼去掉,jsp頁(yè)面不會(huì)亂碼的。
          請(qǐng)幫一下了,我的代碼如下:

          package example;

          import javax.servlet.http.HttpServletResponse;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.ActionSupport;


          public class Login extends ActionSupport {


          public String execute() throws Exception {
          String str = "";
          str += "<script language=javascript>" + "\n";
          str += "alert('hello,world!');" + "\n";
          str += "</script>";
          HttpServletResponse response = ServletActionContext.getResponse();
          response.getWriter().println(str);
          return SUCCESS;
          }


          } 


          jsp頁(yè)面:
          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <html>
          <head>
          <title>Sign On</title>
          </head>

          <body>
          <s:form action="Login" >
          中文用戶名:<s:textfield name="username" />
          密碼: <s:password name="password" />
          <s:submit/>

          </s:form>
          </body>
          </html>
            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-06-10 14:54 | jbas
          return SUCCESS;
          改成
          return null;

          是可以了  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-08-26 20:54 | henry1451
          落伍了啊,max兄06年就接處了struts2.0,而我到08年4月份才接處到.  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-09-03 11:41 | walkes
          我要那些用到的包啊  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-09-03 11:54 | walkes
          commons-digester-1.8.jar
          貌似還要這個(gè)包才行 否則vmlocity例子有問(wèn)題  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2008-09-09 15:39 | tangyong
          multiple operations have reported errors.

          樓主知道這個(gè)錯(cuò)誤是什么么?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2008-11-07 00:18 | lonqi
          真是一篇好文章,謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-01-24 17:01 | 小鼬
          不錯(cuò),控制反轉(zhuǎn)有點(diǎn)難……  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-02-20 15:56 | xuxu
          你老板說(shuō)的也是扯淡

          寫(xiě)的程序無(wú)非就是對(duì)數(shù)據(jù)庫(kù)的增刪改查  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-02-24 16:17 | unique_zxs
          李剛就是你吧!《Struts2權(quán)威指南》的作者。  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-03-06 17:16 | fancyLeeo
          問(wèn)個(gè)問(wèn)題:
          如何才能通過(guò)JSP頁(yè)面用Struts標(biāo)簽取得message?
            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-03-06 17:30 | fancyLeeo
          呵呵,自己回答吧,剛犯了一個(gè)小錯(cuò)誤。

          <s:property value="message" />

            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-03-06 17:31 | fancyLeeo
          @xuxu
          你也是扯蛋,你說(shuō)的只是一部分;

          程序真的無(wú)非就是輸入、處理、輸出!

          程序最終也真的無(wú)非就是抽象……  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-03-10 15:22 | fdsa
          fdsafdsa  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-03-10 15:22 | fdsa
          @fdsa
          fdssafdsafsadfds  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-03-26 19:39 | kghzjx
          ActionContext.getContext().getSession()獲取并不是是HttpSession而是一個(gè)HashMap對(duì)象實(shí)例
            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-04-23 12:01 | marin
          自定義類  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-09-09 22:47 | dfa
          asdfasdf
            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2009-09-09 22:49 | ticky
          我想用Struts2.0架構(gòu)做一個(gè)東西,我有一個(gè)數(shù)據(jù)庫(kù),里面有好幾個(gè)表,我要對(duì)表進(jìn)行查詢?nèi)缓笾付ㄒ獎(jiǎng)h除的記錄。現(xiàn)在的問(wèn)題是,我查詢后怎么寫(xiě)代碼可以把記錄都列出來(lái),因?yàn)椴樵儣l件不同的話是不能預(yù)知記錄條數(shù)的。。我的信箱tarbaobao@163.com。謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解[未登錄](méi) 2009-12-11 12:17 | xx
          User是一個(gè)實(shí)體類,這樣寫(xiě)實(shí)實(shí)現(xiàn)對(duì)象模型@jintian
            回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2010-11-10 23:29 | renhong
          Max的功底真的從代碼都看出來(lái)了。第一次看到這么規(guī)范的代碼。。。具有美感  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2011-03-08 12:47 | taokai
          @jintian
          User 類當(dāng)然是自己創(chuàng)建的啊, 是把屬性: name和password 封裝到User類中去了, 封裝這個(gè)思想你明白?  回復(fù)  更多評(píng)論
            
          # re: Struts 2.0的Action講解 2011-05-11 14:20 | 宇穹
          樓主,你好。我正在用struts2做畢設(shè) 關(guān)于ACTION類遇到個(gè)很不解的地方希望你指導(dǎo):
          在提交表單內(nèi)容后,即是在Action類中包含的Bean中已經(jīng)getter 和setter,服務(wù)器總會(huì)報(bào)錯(cuò),提示“ognl.OgnlException: target is null for setProperty(null, "property", [Ljava.lang.String:@2312]”,其中property為Bean的一個(gè)屬性值。并不是所有屬性值都不能傳遞,僅有個(gè)別屬性值會(huì)出現(xiàn)這樣的情況。
          網(wǎng)上搜到一個(gè)解決方法是將Action中的Bean類設(shè)置為public,然后問(wèn)題解決。但是不明白在同一個(gè)包中有的Action類Bean類為private會(huì)出現(xiàn)上述問(wèn)題,而有的Action類包含的Bean類同為private卻不會(huì)出現(xiàn)問(wèn)題,很困惑,請(qǐng)博主指教。(注:Action包中只含有Action類,所有Bean類統(tǒng)一放在另外的一個(gè)包中)
          qq 272253421 望指教  回復(fù)  更多評(píng)論
            
          評(píng)論共2頁(yè): 1 2 下一頁(yè) 
          主站蜘蛛池模板: 郴州市| 竹山县| 广汉市| 武平县| 五家渠市| 郸城县| 昌图县| 东乌珠穆沁旗| 黄石市| 宜兰县| 葫芦岛市| 来凤县| 大埔县| 巴青县| 新和县| 隆德县| 维西| 旅游| 任丘市| 屯门区| 华阴市| 揭阳市| 平阳县| 靖州| 大港区| 静宁县| 原阳县| 讷河市| 兴义市| 开封市| 石景山区| 大姚县| 巢湖市| 杂多县| 屏南县| 江油市| 潞西市| 富蕴县| 密云县| 太仓市| 郴州市|