继承ActionSupport例子

ActionSupport例子,是基于我上一个博客-菜鸟关于Sturdy框架的搭建 的基础上写的,具体链接https://blog.csdn.net/qq_43561093/article/details/83582403

主要做的是一个登陆验证的例子

1:在src下的com.hnpi.action包下创建一个名为elloWorldAction的.java文件

写入以下示例代码


package com.hnpi.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;
    }  
    
}  


2:写页面代码,在WebRoot下的Index的.jsp文件中写入以下代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>信息展示页面</title>
  </head>
  
  <body>
    用户名:${requestScope.name}<br />
    性别:${requestScope.sex}<br />
  </body>
</html>

   3:在WebRoot下创建名为register的.jsp文件,写入代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>录入页面</title>
    
  </head>
  
  <body>
   <form action="test/register" method="post">
   用户名:<input type="text" name="name"/><br /> 
   性别:<input type="text" name="sex"/><br /> 
   <input type="submit" value="提交">
   </form>
  </body>
</html>

 4:在WebRoot下创建名为login的.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="test/helloworldAction.action" 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>  

 5:在WebRoot下创建名为welcom的.jsp文件,写入代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>显示页面</title>

  </head>
  
  <body>
    登陆成功
  </body>
</html>

6:寻找WebRoot下的WEB-INF下的lib下名为web的.xml文件,修改代码参考如下:

主要修改代码为:org.apache.struts2.dispatcher.ng.filter,需要按照这个路径寻找地址,希望可以牢记这一点。

<?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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  <filter-name>struts</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter implements</filter-class>
</filter>

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

</web-app>

7:重头戏来了,我们的继承ActionSupport继承要来了,我们要

在src下的com.hnpi.action包下创建一个名为RegisAction的.java文件

在建立时做以下修改

 

8:我这也是边做边摸索的,长长的代码如下:

package com.hnpi.action;

public class RegisterAction {

	private String name;
	private String sex;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public String t1(){
		return "t1";
	}
}

9:在src下建一个名为struts的.xml的文件,将第二个java文件写入里面,具体看图:

10:写一个名为welcom的》jsp的登陆成功的页面,看图:

 

11;哈哈哈哈,让我们结束这个工程吧,加入jar包,进行配置,运行文件,放弃修仙:

 

 

 声明:以上出现的代码源于上课文件,截图来源于老师控屏所截,可用于个人练习,商业用途必究

  注:最终解释权为于飞草所有。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值