首页 > 代码库 > 简单的Struts2例子

简单的Struts2例子

(1)用MyEclipse建立一个Web Project,项目名称为myStruts2,结构如下图:

(2)导入struts2需要的包,我是把解压后的struts-2.3.16.3\apps\struts2-blank\WEB-INF\lib\下面所有的包都导入;

(3)修改web.xml,设置filter和filter-mapping:

<?xml version="1.0" encoding="GB2312"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    </filter>        <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>      <welcome-file-list>        <welcome-file>index.jsp</welcome-file>      </welcome-file-list></web-app>

(4)在WebRoot\jsp\下面新建三个jsp文件:

input.jsp,用于输入信息

<%@ page language="java" pageEncoding="gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- struts2标签库调用声明 --><%@taglib prefix="s" uri="/struts-tags"%><html><head>    <title>输入页面</title></head><body>    <!-- form标签库定义,以及调用哪个Action声明 -->    <s:form action="INPUT">        <table width="60%" height="76" border="0">                <!-- 各标签定义 -->                <s:textfield name="name" label="姓名"/>                <s:textfield name="phone" label="电话"/>                <s:textfield name="mail" label="邮箱"/>                <s:submit value="确定" align="center"/>                        </table>    </s:form></body></html>

list.jsp,用于列出输入的信息

<%@ page language="java" contentType="text/html; charset=GB2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>   <title>输入成功</title></head>  <body>    <!-- 取得session中用户名值 -->        姓名:${sessionScope.name}<br>        电话:${sessionScope.phone}<br>        邮箱:${sessionScope.mail}</body></html>

error.jsp,用于出错提示:

<%@ page language="java" contentType="text/html; charset=GB2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>   <title>输入失败</title></head>  <body>        失败,请重新输入!</body></html>

(5)创建struts.xml文件,设置package和Action:

<?xml version="1.0" encoding="gb2312"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <!-- Action所在包定义 -->    <package name="myStruts2" extends="struts-default">        <!-- 全局导航页面定义 -->        <global-results>            <result name="global">/jsp/error.jsp</result>        </global-results>    <!-- Action名字,类以及导航页面定义 -->        <!-- 通过Action类处理才导航的的Action定义 -->        <action name="INPUT"            class="myStruts2.InputAction">            <result name="input">/jsp/input.jsp</result>            <result name="list">/jsp/list.jsp</result>            <result name="error">/jsp/error.jsp</result>        </action>        <!-- 直接导航的的Action定义 -->        <action name="index" >            <result >/jsp/input.jsp</result>                    </action>    </package></struts>

(6)创建InputAction类:

package myStruts2;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class InputAction extends ActionSupport {    // Action类公用私有变量,用来做页面导航标志    private static String FORWARD = null;    // 姓名,电话,邮箱    private String name;    private String phone;    private String mail;    public String getName() {        return name;    }    public void setName(String x) {        this.name = x;    }    public String getPhone() {        return phone;    }    public void setPhone(String x) {        this.phone = x;    }        public String getMail() {        return mail;    }    public void setMail(String x) {        this.mail = x;    }    public String execute() throws Exception {                // 属性值即JSP页面上输入的值        name = getName();        phone = getPhone();        mail = getMail();        try {            // 判断输入值是否是空对象或没有输入            if (name != null && !name.equals("")                     && phone != null && !phone.equals("")                    && mail != null && !mail.equals(""))             {                ActionContext.getContext().getSession().put("name", getName());                ActionContext.getContext().getSession().put("phone", getPhone());                ActionContext.getContext().getSession().put("mail", getMail());                // 根据标志内容导航到操作成功页面                FORWARD = "list";            } else {                // 根据标志内容导航到操作失败页面                FORWARD = "error";            }        } catch (Exception ex) {            ex.printStackTrace();        }        return FORWARD;            }}

(7)部署到Tomcat后运行,结果如下: