下载此Struts ForwardAction示例– Struts-ForwardAction-Example.zip
在Struts MVC模型中,您必须考虑动作控制器才能获得新的视图页面。 在某些情况下,您实际上仅需要获取指定的JSP页面,因此创建一个仅将页面转发给您的动作控制器类太愚蠢了,例如
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
<action path="/Welcome"
type="com.mkyong.common.action.WelcomeAction">
<forward name="success" path="/Welcome.jsp"/>
</action>
Struts带有一个名为ForwardAction (org.apache.struts.actions.ForwardAction)的特殊动作控制器类,可以按照名称描述执行“ 仅转发 ”任务,并允许您直接访问指定的JSP页面。
例
声明一个“ / Welcome ” Web路径,将属性键入为ForwardAction类,然后将其转发到Welcome.jsp页面。
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action
path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/Welcome.jsp"/>
</action-mappings>
</struts-config>
Welcome.jsp
This is Welcome Page
创建一个index.jsp页面,当用户单击链接时,它将转发到“ / Welcome ”(返回Welcome.jsp)
ForwardAction Example
page="/Welcome.do">
http:// localhost:8080 / StrutsExample / ,单击链接。
它将转发到http:// localhost:8080 / StrutsExample / Welcome.do
标签: Struts
翻译自: https://mkyong.com/struts/struts-forwardaction-example/