继承ActionSupport
以之前Struts2框架为基础创建一个ActionSupport
1,首先创建一个src下新的class,图片如下;
其中部分代码如:
package com.hnpi.action;
import com.opensymphony.xwork2.ActionSupport;
public class HellowWorldAction 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,2.1创建两个所需的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>My JSP 'index.jsp' starting page</title>
</head>
<body>
登陆成功。
</body>
</html>
2.2
其中代码如下:
<%@ 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="text/hellowWorldAction" 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>
3,配置Struts.xml的代码如下图:
红线内部为要添加的新内容。
代码如下:
<action name="hellowWorldAction" class="com.hnpi.action.HellowWorldAction">
<result name="toWelcome">/welcome.jsp</result>
<result name="input">/login.jsp</result>
</action>
!!!在进行编码时特别要注意路径问题。
继承ActionSupport例子的主要代码就这些啦,如有错误请留言!