Struts2继承ActionSupport
打开自己的MyEclipse,新建一个普通的javaweb项目,如图所示:
2.视图层 jsp页面,如下图:
代码如下:
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<%@ taglib prefix=“s” uri="/struts-tags"%>
账号:
密码:
<%@ 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 类,如下图:
代码如下:
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包,如下图:
5.在web.xml中配置Struts2的过滤器,代码如下:
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet
com.hnpi.controller.TestServlet
代码如下:<?xml version="1.0" encoding="UTF-8"?>
/welcome.jsp /login.jsp以上内容仅供参考,如有内容错误,请留言讨论。