为什么我需要打开 blah.do 才能被实际程序占用?
Why do I need to open the blah.do before it is taken by the actual program?
我在学校学习 struts。我有一个非常基本的 hello world,但有一件事我似乎没有做对。
我有支撑形式、支撑动作、主要 jsp(从 welcomeStruts.jsp 开始工作)和 struts-config.xml..是我正在处理的四个文件。
表格是这样的:
public class mensaje extends org.apache.struts.action.ActionForm {
private String message;
public String getMessage() {
return message;
}
public void setMessage (String message) {
this.message = message;
}
}
动作是这样的:
public class mensajeAction extends org.apache.struts.action.Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
mensaje hellwForm = (mensaje) form;
hellwForm.setMessage("Hello World");
return mapping.findForward("success");
}
}
来自jsp的调用是这样的:
<html:form action="/mensajeAct">
<bean:write name="mensaje" property="message"/>
</html:form>
最后,这就是我在 struts-config
中的内容
<form-beans>
<form-bean name="mensaje" type="Struts.Form.mensaje"/>
</form-beans>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
现在...如果我只是转到我的浏览器 (localhost:8080/helloworld),它不会添加操作的 "Hello World" 部分。
但是,如果我先在浏览器中转到 .do (localhost:8080/helloworld/mensajeAct.do),然后返回主页 (localhost:8080/helloworld),它现在可以工作了。此时,在我访问 .do 一次之后,我可以在 netbeans 中进行更改,点击播放,它会接受更改。
所以,问题是,我在 struts 中做错了什么,还是 GlassFish 在耍我?我试过 IE 和 Firefox,两者的行为相同。
非常感谢!
您的 *.do
应与 <action-mappings>
中的 path
对应。因此 url localhost:8080/helloworld/mensajeAct.do
中的 mensajeAct.do
与 path="/mensajeAct"(<action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>)
映射,它会将您带到相关的操作 class(在您的情况下为 mensajeAction
)
步骤:
- RequestProcessor 使用
mensajeAct.do
查找 xml 块
- 它从
struts-config.xml
<action
input="/Welcome" name="mensaje" path="/mensajeAct" scope="session"
type="Struts.Action.mensajeAction" validate="false"/>
中找到以下 xml 块
- 它实例化了你的 Action class(
mensajeAction
)
type
属性
- 在操作 class 中执行必要的步骤后,它会搜索
<forward name="success" path="blah.jsp"/>
在你的
<action-mappings>
并转发到 blah.jsp
在上面 mensajeAction 操作 class 中的代码中,您转发了对 "success" servelet 的操作响应,该服务未映射到您的 struts-config.xml。
根据 Struts 1.0 规范,它需要映射,因为对于 struts 中的每个请求和响应,请求处理器都会查看 struts-config.xml,因为它是主要的控制器配置。
希望对您有所帮助!
实际答案是因为 Struts 1.x 框架被设计为 运行 在 Servlet 容器(例如 Apache Tomcat)中。
所有 Servlet 容器都遵循基本原则,即您的 Web 应用程序 必须 包含一个 web.xml
文件。要使 Struts 正常工作,它遵循如下简单的配置:
<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>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<url-pattern>
表示所有以 .do
结尾的 URL 部分必须由 Servlet 容器拾取并调用一个 servlet 名称为 action
的 servlet。这个action
就是你的strutsActionServlet
。这是将责任委托给 Struts RequestProcessor
和 运行 您的 Action
class.
的实际处理者
这种类型的架构称为 Model View Controller
模式,这就是 Struts 1.x 的设计基础。
本质上,如果您的 web.xml
文件中没有 <servlet>
和 <servlet-mapping>
,映射到您的 Struts' ActionServlet
,您的应用程序将不会完全被执行。
我在学校学习 struts。我有一个非常基本的 hello world,但有一件事我似乎没有做对。
我有支撑形式、支撑动作、主要 jsp(从 welcomeStruts.jsp 开始工作)和 struts-config.xml..是我正在处理的四个文件。
表格是这样的:
public class mensaje extends org.apache.struts.action.ActionForm {
private String message;
public String getMessage() {
return message;
}
public void setMessage (String message) {
this.message = message;
}
}
动作是这样的:
public class mensajeAction extends org.apache.struts.action.Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
mensaje hellwForm = (mensaje) form;
hellwForm.setMessage("Hello World");
return mapping.findForward("success");
}
}
来自jsp的调用是这样的:
<html:form action="/mensajeAct">
<bean:write name="mensaje" property="message"/>
</html:form>
最后,这就是我在 struts-config
中的内容 <form-beans>
<form-bean name="mensaje" type="Struts.Form.mensaje"/>
</form-beans>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
现在...如果我只是转到我的浏览器 (localhost:8080/helloworld),它不会添加操作的 "Hello World" 部分。 但是,如果我先在浏览器中转到 .do (localhost:8080/helloworld/mensajeAct.do),然后返回主页 (localhost:8080/helloworld),它现在可以工作了。此时,在我访问 .do 一次之后,我可以在 netbeans 中进行更改,点击播放,它会接受更改。
所以,问题是,我在 struts 中做错了什么,还是 GlassFish 在耍我?我试过 IE 和 Firefox,两者的行为相同。
非常感谢!
您的 *.do
应与 <action-mappings>
中的 path
对应。因此 url localhost:8080/helloworld/mensajeAct.do
中的 mensajeAct.do
与 path="/mensajeAct"(<action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>)
映射,它会将您带到相关的操作 class(在您的情况下为 mensajeAction
)
步骤:
- RequestProcessor 使用
mensajeAct.do
查找 xml 块
- 它从
struts-config.xml
<action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>
中找到以下 xml 块
- 它实例化了你的 Action class(
mensajeAction
)type
属性 - 在操作 class 中执行必要的步骤后,它会搜索
<forward name="success" path="blah.jsp"/>
在你的<action-mappings>
并转发到blah.jsp
在上面 mensajeAction 操作 class 中的代码中,您转发了对 "success" servelet 的操作响应,该服务未映射到您的 struts-config.xml。 根据 Struts 1.0 规范,它需要映射,因为对于 struts 中的每个请求和响应,请求处理器都会查看 struts-config.xml,因为它是主要的控制器配置。
希望对您有所帮助!
实际答案是因为 Struts 1.x 框架被设计为 运行 在 Servlet 容器(例如 Apache Tomcat)中。
所有 Servlet 容器都遵循基本原则,即您的 Web 应用程序 必须 包含一个 web.xml
文件。要使 Struts 正常工作,它遵循如下简单的配置:
<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>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<url-pattern>
表示所有以 .do
结尾的 URL 部分必须由 Servlet 容器拾取并调用一个 servlet 名称为 action
的 servlet。这个action
就是你的strutsActionServlet
。这是将责任委托给 Struts RequestProcessor
和 运行 您的 Action
class.
这种类型的架构称为 Model View Controller
模式,这就是 Struts 1.x 的设计基础。
本质上,如果您的 web.xml
文件中没有 <servlet>
和 <servlet-mapping>
,映射到您的 Struts' ActionServlet
,您的应用程序将不会完全被执行。