struct1 页面跳转配置Demo
来源:互联网 发布:mysql查询最小时间 编辑:程序博客网 时间:2024/06/15 04:11
一、web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.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><load-on-startup>2</load-on-startup></servlet><servlet-mapping> <!-- 控制器名称 --><servlet-name>action</servlet-name><!-- 作用范围,所有以.do结尾的action--><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
二、structs-config.xml配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- 封装提交的参数 -->
<form-beans>
<form-bean name="registerForm" type="cn.itcast.web.struts1.form.RegisterForm"></form-bean>
<form-bean name="loginForm" type="cn.itcast.web.struts1.form.LoginForm"></form-bean>
</form-beans>
<action-mappings>
<!-- path表示servlet控制的action, type表示action所转向的类-->
<action path="/register" type="cn.itcast.web.struts1.action.RegisterAction"
name="registerForm" scope="request" />
<!--
include表示所要包含的页面,将所要包含的页面包含进来,不能与forward同时使用
path为form中action的路径,省略了web.xml配置中<url-pattern>*.do</url-pattern>的后缀.do
-->
<action path="/login1" include="/welcome.jsp" name="loginForm"
scope="request" />
<!--
forward表示所要转向的页面,不能与include同时使用
-->
<action path="/login2" forward="/register.jsp" name="loginForm"
scope="request" />
<!--
action中无include或forward时,forward可做为action的子元素,forward的参数name与LoginAction中的mapping.getForward("success")相同,
path为此参数对应跳转,redirect为true时表示重定向
-->
<action path="/login" name="loginForm"
type="cn.itcast.web.struts1.action.LoginAction" scope="request">
<forward name="success" path="/success.jsp" redirect="false" />
<forward name="failure" path="/register.jsp" redirect="true" />
</action>
</action-mappings>
</struts-config>
三、收集参数的FormBean继承ActionForm类,实现如下
package cn.itcast.web.struts1.form;import org.apache.struts.action.ActionForm;public class LoginForm extends ActionForm{private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
package cn.itcast.web.struts1.form;import org.apache.struts.action.ActionForm;public class RegisterForm extends ActionForm{private String username;private String password;public void setUsername(String username) {this.username = username;System.out.println("框架自动注入"+username);}public void setPassword(String password) {this.password = password;System.out.println("框架自动注入"+password);}public String getUsername() {return username;}public String getPassword() {return password;}}
四、自定义的action方法继承Action类,并实现其方法
package cn.itcast.web.struts1.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import cn.itcast.web.struts1.form.LoginForm;public class LoginAction extends Action {@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {request.setCharacterEncoding("utf-8");LoginForm loginForm = (LoginForm) form;String username = loginForm.getUsername();System.out.println("username:" + username);if(username.equals("mary")){return mapping.findForward("success");}else{return mapping.findForward("failure");}}}
package cn.itcast.web.struts1.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import cn.itcast.web.struts1.form.RegisterForm;//分控制器public class RegisterAction extends Action{@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {request.setCharacterEncoding("utf-8");RegisterForm registerForm = (RegisterForm) form;String username =registerForm.getUsername();String password = registerForm.getPassword();System.out.println(username+":"+password);return new ActionForward("/success.jsp",false);}}
- struct1 页面跳转配置Demo
- struct1开发入门Demo
- struct1
- 配置错误跳转页面
- Android项目页面跳转小Demo
- struts配置中,页面跳转
- nginx配置404跳转页面
- springboot配置跳转html页面
- 非ajax提交页面不跳转,java调用javascript demo
- iOS小demo之跳转到特定页面
- iOS小demo之跳转系统页面和APPStore
- iis 中配置 域名登录 跳转页面
- 配置nginx的403错误页面跳转
- 在web.xml配置错误信息跳转页面
- struts2 异常跳转页面配置
- EasyUI+SpringMVC配置session过期跳转页面
- Filter配置会话超时跳转页面
- web.config文件配置错误跳转页面
- Gabor的OpenCV代码
- tomcat优化-有改protocol 和 缓存 集群方案 转载自http://passover.blog.51cto.com/2431658/732629
- 网络通信引擎--C++ 与 lua
- asp.net中后台c#数组与前台js数组交互
- 安卓开发的心得体会
- struct1 页面跳转配置Demo
- struts2 Result type
- 病毒样本分析小结
- AIX中常用的SMIT 的使用
- ZOJ-2545
- 设计模式精粹
- 装饰者模式
- ExtJS引入问题
- node开发,一个request中多次访问数据库再返回结果