Struts ForwardAction Example
2015-08-28 13:33
761 查看
In Struts MVC model, you have to go thought the Action Controller to get a new view page. In some cases, you really just need to get a specified JSP page only, it’s so stupid to create an action controller class which just forward the page to you, for example
Struts comes with a special action controller class called
Create a
![](https://oscdn.geek-share.com/Uploads/Images/Content/202001/22/4752c594757ea39a5e6c60e28cc42ad6.jpg)
It will forward to http://localhost:8080/StrutsExample/Welcome.do
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 comes with a special action controller class called
ForwardAction(
org.apache.struts.actions.ForwardAction), to do the “
forward-only” task as name described, and allow you to access the specified JSP page directly.
Example
Declare a “/Welcome” web path,
typeattribute as
ForwardActionclass, and forward it to
Welcome.jsppage.
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
Create a
index.jsppage, when user click on the link, it will forward to “
/Welcome” (return
Welcome.jsp)
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> ForwardAction Example Click me to access to JSP Welcome page
http://localhost:8080/StrutsExample/, click on the link.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202001/22/4752c594757ea39a5e6c60e28cc42ad6.jpg)
It will forward to http://localhost:8080/StrutsExample/Welcome.do
![](https://oscdn.geek-share.com/Uploads/Images/Content/202001/22/f15af7e155a370d1915b5c514f826d40.jpg)
相关文章推荐
- SSH问题(五)java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
- eclipse mylyn.tasks.ui
- Struts – Wildcards example
- struts2 if标签示例
- Struts – Multiple configuration files example
- Web Service-- 使用 JDK 发布 WS
- Configure a welcome page in Struts
- java -日期
- java正则表达式练习
- Configure the Struts Tag Libraries
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- eclipse下使用git插件上传代码至github (或下载代码)
- 使用java理解程序逻辑,变量
- Java Socket例程3 UDP
- java状态模式
- Java常用排序
- Struts Hello World Example
- Spring面试题
- Windows平台上,使用eclipse连接到DB2数据库
- 刚刚开始学java,我的第一个比较好一点的Java程序。ps:只要努力,就会有收获