Struts2继承ActionSupport类

今天为大家带来一个创建Struts2并且继承他的ActionSupport类
以下以一个小例子来展示其过程
老样子,首先打开我们的Myeclipse编写软件
创建一个大家自定义名字的javaweb工程并且在工程的src目录下常见servlet项目
我已经经创好了,如下图所示
在这里插入图片描述
其中包名与servlet项目名大家按照自己习惯书写即可
在创建helloWorldAction时记得Supecclass中点击browse中搜索ActionSupport如下图所示选择第一个
在这里插入图片描述
helloWorldAction下代码如下

package cn.javass.hello.struts2impl.action;  

import com.opensymphony.xwork2.ActionSupport;
  
public class HelloWorldAction extends ActionSupport {  
    private String account;  
    private String password;  
    private String submitFlag;  
    public String execute() throws Exception {  
        this.businessExecute();  
        return "toWelcome";  
    }  
    public void validate(){  
        if(account==null || account.trim().length()==0){  
            this.addFieldError("account", "账号不可以为空");  
        }  
        if(password==null || password.trim().length()==0){  
            this.addFieldError("password", "密码不可以为空");  
        }
        if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){  
            this.addFieldError("password", "密码长度至少为6位");  
        }  
    }  
    /** 
     * 示例方法,表示可以执行业务逻辑处理的方法, 
     */  
    public void businessExecute(){  
        System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);  
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSubmitFlag() {
        return submitFlag;
    }
    public void setSubmitFlag(String submitFlag) {
        this.submitFlag = submitFlag;
    }  
    
}  

然后在WepRoot创建一个注册页面的jsp代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; utf-8">  
<title>Insert title here</title>  
<style type="text/css">
ul,li {
    list-style-type:none;
    margin:0px;
    float:left;
}
</style>
</head>  
<body>  
   
<form action="helloworldAction" method="post"> 
    <input type="hidden" name="submitFlag" value="login"/>  
    <div> 
        <font color=red><s:fielderror fieldName="account"/></font>
        <br/>
          账号:<input type="text" name="account">
    </div>
    <div>
        <font color=red><s:fielderror fieldName="password"/></font>
        <br/>
            密码:<input type="password" name="password">
    </div>
    <input type="submit" value="提交">  
</form>  
  
</body>  
</html>  

把struts2所需要的配置文件放入lib目录下
在这里插入图片描述
在src页面下新建一个struts.xml的servlet文件代码如下
代码完成后保存如果struts目录出现小齿轮表示创建符合要求如下图
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  
 <struts>
    <package name="default"  extends="struts-default">  
        <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">  
            <result name="toWelcome">/welcome.jsp</result> 
             <result name="input">/login.jsp</result>   
        </action>  
    </package>    
 </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">
    
 <welcome-file-list>
 <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>
 
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>

 <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>

大家注意jsp中的form表单的action要与struts的的name属性名相同
另外过滤器的filter-class:打开目录按照以下路径查看即可org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
在这里插入图片描述
最后为大家奉上效果图
在这里插入图片描述
以上一个Struts2继承ActionSupport类的小例子已经完成了 当然中间还有许多知识点,我也是一个小白,只能勉勉强强做下来,其中如有不足,大家多多包涵,如有意见请告诉我,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值