2012-04-03 45 views
-1

當學習使用struts編寫基本應用程序時,我遇到了以下可正常工作的代碼。整個事情是如何運作的?

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
<servlet> 
    <servlet-name>action</servlet-name> 
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
    <init-param> 
     <param-name>config</param-name> 
     <param-value>/WEB-INF/struts-config.xml</param-value> 
    </init-param> 
    <init-param> 
     <param-name>debug</param-name> 
     <param-value>2</param-value> 
    </init-param> 
    <init-param> 
     <param-name>detail</param-name> 
     <param-value>2</param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

的index.jsp

jsp:forward page="HelloWorld.do"/> 

的struts-config.xml

<struts-config> 

<form-beans> 
<form-bean name="HelloWorldActionForm" 

類型= 「com.vaannila.HelloWorldActionForm」/>

<action-mappings> 
<action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld" scope="session" type="com.vaannila.HelloWorldAction"> 
<forward name="success" path="/helloWorld.jsp" /> 
</action> 
</action-mappings> 

HelloWorldActionForm

public class HelloWorldActionForm extends 
org.apache.struts.action.ActionForm { 

private String message; 

public String getMessage() { 
    return message; 
} 

public void setMessage(String Message) { 
    message = Message; 
} 

} 

HelloWorldAction.java

public class HelloWorldAction extends org.apache.struts.action.Action { 

private final static String SUCCESS = "success"; 

public ActionForward execute(ActionMapping mapping,ActionForm form, 
HttpServletRequest request,HttpServletResponse response) throws Exception { 

HelloWorldActionForm helloWorldForm = (HelloWorldActionForm) form; 
helloWorldForm.setMessage("Hello World!"); 
return mapping.findForward(SUCCESS); 
} 
} 

的helloWorld.jsp

Hey ! It's me from struts. 

應用程序工作正常。但我不明白如何請求HelloWorld.do終於到達helloWorld.jsp後我打開index.jsp?我明白HelloWorldAction實際上返回頁面的東西。

+1

下面是流程的流程:當你訪問index.jsp時,請求被轉發到H elloWorld.do(請參閱jsp:forward)。看到.do模式的應用程序服務器將請求傳遞給ActionServlet(請參閱:web.xml)。現在ActionServlet已經配置了struts-config.xml文件。配置文件有一個爲/ HelloWorld定義的動作。因此,請求由HelloWorldAction處理,它返回'success'代碼 - 它被映射到helloWorld.jsp(再次指向struts-config.xml),它是由ActionServlet'controller'返回的'view'。希望有所幫助。 – ben 2012-04-03 19:06:28

+1

你應該把它作爲答案:) – 2012-04-03 19:07:31

+0

http://struts.apache.org/2.0.6/docs/the-struts-2-request-flow.html – ben 2012-04-03 19:07:43

回答

0

當您HelloWorldAction.execute回報率(有效的字符串)「成功」

THEN

<forward name="success" path="/helloWorld.jsp" /> 

比賽「成功」,並提供「/helloWorld.jsp」

+0

在標籤中''_name_屬性告訴了什麼? – 2012-04-04 04:07:49

相關問題