1.创建一个JavaWeb项目,编写一个Action对象,其名为GreetingAction,继承ActionSuppport类
具体代码如下:
import com.opensymphony.xwork2.ActionSupport;
public class GreetingAction extends ActionSupport {
private static final long serialVersionUID = 1L;
// 用户名
private String username;
// 处理请求
@Override
public String execute() throws Exception{
// 判断用户名是否有效
if(username == null || "".equals(username)){
// 返回到错误页面
return ERROR;
}else{
// 返回到成功页面
return SUCCESS;
}
}
// username属性的getter方法
public String getUsername() {
return username;
}
// username属性的setter方法
public void setUsername(String username) {
this.username = username;
}
}
2.在web-inf目录下创建web.xml文件,为Struts2配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>8.2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list><!-- Struts2过滤器 -->
<filter>
<!-- 过滤器名称 -->
<filter-name>struts2</filter-name>
<!-- 过滤器类 -->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- Struts2过滤器映射 -->
<filter-mapping>
<!-- 过滤器名称 -->
<filter-name>struts2</filter-name>
<!-- 过滤器映射 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.在src目录下创建struts.xml文件,并写入以下代码
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default">
<!-- 定义action -->
<action name="greeting" class="com.lyq.action.GreetingAction">
<!-- 定义成功的映射页面 -->
<result name="success">success.jsp</result>
<!-- 定义失败的映射页面 -->
<result name="error">error.jsp</result>
</action>
</package>
</struts>
4.创建一个jsp页面,在该页面中编写一个简单的表单
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页</title>
<style type="text/css">
*{font-size: 12px;}
</style>
</head>
<body>
<form action="greeting.action" method="post">
请输入你的姓名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>
5.创建Greeting类处理成功所返回的页面和处理失败所返回的页面:success.jsp、error.jsp
success.jps:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
<style type="text/css">
*{font-size: 12px;}
</style>
</head>
<body>
<font color="red">
<s:property value="username" />
</font>
,您好!
<br>
欢迎使用来到本站。
</body>
</html>
error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误</title>
</head>
<body>
<font color="red"> 错误:您没有输入姓名!</font>
</body>
</html>