(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é)果如下:


新聞熱點(diǎn)
疑難解答
圖片精選