当学习使用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
实际上返回页面的东西。
下面是流程的流程:当你访问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
你应该把它作为答案:) – 2012-04-03 19:07:31
http://struts.apache.org/2.0.6/docs/the-struts-2-request-flow.html – ben 2012-04-03 19:07:43