整个过程如何运作?

时间:2012-04-03 18:53:05

标签: java jsp struts

当学习使用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”/&GT;     

<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 个答案:

答案 0 :(得分:0)

HelloWorldAction.execute 返回(生效字符串)“成功”

然后

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

匹配“成功”并发送“/ helloWorld.jsp”