Struts2继承ActionSupport

打开自己的MyEclipse,新建一个普通的javaweb项目,如图所示:
Struts2继承ActionSupport
2.视图层 jsp页面,如下图:
Struts2继承ActionSupport
代码如下:
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<%@ taglib prefix=“s” uri="/struts-tags"%>

Insert title here

账号:

密码:

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

<title>欢迎页面</title>
欢迎登陆

3.新建一个控制层 HelloWorldAction 类,如下图:
Struts2继承ActionSupport
代码如下:
package com.hnpi.controller;

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;
}  

}

4.4.在web-inf 下的lib 里边添加Struts2所需要的jar包,如下图:
Struts2继承ActionSupport
5.在web.xml中配置Struts2的过滤器,代码如下:

<?xml version="1.0" encoding="UTF-8"?>



This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet
com.hnpi.controller.TestServlet

TestServlet /TestServlet struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* **6.在src下新建一个请求分发的配置文件 struts.xml,如下图:** ![在这里插入图片描述](https://img-blog.****img.cn/2018103120363037.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTcxMDAx,size_16,color_FFFFFF,t_70)

代码如下:<?xml version="1.0" encoding="UTF-8"?>

/welcome.jsp /login.jsp

以上内容仅供参考,如有内容错误,请留言讨论。