国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Struts2例子

2019-11-14 20:53:45
字體:
供稿:網(wǎng)友
Struts2例子

(1)用MyEclipse建立一個(gè)Web PRoject,項(xiàng)目名稱為myStruts2,結(jié)構(gòu)如下圖:

(2)導(dǎo)入struts2需要的包,我是把解壓后的struts-2.3.16.3/apps/struts2-blank/WEB-INF/lib/下面所有的包都導(dǎo)入;

(3)修改web.xml,設(shè)置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/下面新建三個(gè)jsp文件:

input.jsp,用于輸入信息

<%@ page language="java" pageEncoding="gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- struts2標(biāo)簽庫調(diào)用聲明 --><%@taglib prefix="s" uri="/struts-tags"%><html><head>    <title>輸入頁面</title></head><body>    <!-- form標(biāo)簽庫定義,以及調(diào)用哪個(gè)Action聲明 -->    <s:form action="INPUT">        <table width="60%" height="76" border="0">                <!-- 各標(biāo)簽定義 -->                <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,用于出錯(cuò)提示:

<%@ 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)創(chuàng)建struts.xml文件,設(shè)置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">        <!-- 全局導(dǎo)航頁面定義 -->        <global-results>            <result name="global">/jsp/error.jsp</result>        </global-results>    <!-- Action名字,類以及導(dǎo)航頁面定義 -->        <!-- 通過Action類處理才導(dǎo)航的的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>        <!-- 直接導(dǎo)航的的Action定義 -->        <action name="index" >            <result >/jsp/input.jsp</result>                    </action>    </package></struts>

(6)創(chuàng)建InputAction類:

package myStruts2;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class InputAction extends ActionSupport {    // Action類公用私有變量,用來做頁面導(dǎo)航標(biāo)志    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());                // 根據(jù)標(biāo)志內(nèi)容導(dǎo)航到操作成功頁面                FORWARD = "list";            } else {                // 根據(jù)標(biāo)志內(nèi)容導(dǎo)航到操作失敗頁面                FORWARD = "error";            }        } catch (Exception ex) {            ex.printStackTrace();        }        return FORWARD;            }}

(7)部署到Tomcat后運(yùn)行,結(jié)果如下:


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临安市| 五莲县| 永仁县| 邵阳县| 通许县| 平顺县| 七台河市| 东乌| 襄城县| 安岳县| 阿拉善盟| 大兴区| 莫力| 德安县| 莆田市| 吉安县| 平陆县| 高淳县| 贵德县| 南汇区| 大埔县| 乌兰浩特市| 平遥县| 长宁区| 岑溪市| 和田县| 宁陵县| 县级市| 富源县| 贡嘎县| 岚皋县| 天峻县| 扶沟县| 密山市| 广南县| 海伦市| 博罗县| 万宁市| 浠水县| 调兵山市| 龙山县|