首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

MyEclipse 圖形化開發 Struts 應用快速入門實戰

     編輯:Java 相關知識

專案創建與 Struts 支持添加

1. 創建 Web 項目

  • File → New → Project → Web Project
  • Project Name: LoginDemo
  • 默認配置點擊 Finish

2. 添加 Struts Capabilities

  • 右鍵項目 → MyEclipse → Add Struts Capabilities...
  • Struts Config Path: 默認 /WEB-INF/struts-config.xml
  • URL Pattern: *.do
  • Default Application Resource: 默認
  • Finish 後自動生成結構並切換到 Plug-in Development Perspective

Add Struts Capabilities 對話框

項目結構

配置標簽庫 (web.xml)

<web-app>
    <!-- Struts 標簽庫定義 -->
    <taglib>
        <taglib-uri>/tags/struts-html</taglib-uri>
        <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/tags/struts-bean</taglib-uri>
        <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/tags/struts-logic</taglib-uri>
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>
    
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

使用 Struts Designer 可視化設計

1. 打開 struts-config.xml → Design 視圖

右側 Palette 面板提供拖拽組件。

2. 先創建成功頁面

  • Palette → JSP File 圖標
  • File Name: userLoginSuccess.jsp
  • Template: Standard JSP using Struts 1.1
  • Finish 後自動註冊到配置文件

創建 JSP 對話框

3. 編輯成功頁面

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html>
<body>
    登錄成功,歡迎 <bean:write name="userName" />!
</body></html>

4. 一鍵生成 Form、Action、JSP

  • Design 視圖空白處右鍵 → New → New Form, Action and JSP
  • Form Bean Name: userLoginForm
  • Action Path: /userLogin
  • Action Class: com.unmi.action.UserLoginAction
  • Form Class: com.unmi.form.UserLoginForm
  • JSP Page: userLogin.jsp

New Form Action JSP 對話框

5. 配置 Form 屬性

  • Optional Details → Form Properties → Add
  • Property 1: userName (Type: String)
  • Property 2: password (Type: String, JSP Input Type: password)

Form Properties 配置

6. 配置 JSP 表單

  • Optional Details → JSP → 勾選 Create JSP Form?
  • Form Action: /userLogin.do
  • Submit Button Label: 登錄

JSP 表單配置

7. 配置 Action 方法與轉向

  • Optional Details → Methods → 取消勾選 validate (簡單演示不驗證)
  • Forwards → Add 兩個轉向:
    • Name: success, Path: /userLoginSuccess.jsp
    • Name: failure, Path: /userLogin.jsp

Methods 配置

Forwards 配置

8. 完成生成

點擊 Finish,Designer 視圖顯示完整流程:

Struts Designer 視圖

微調生成代碼

1. 統一標簽庫聲明

編輯 userLogin.jsp,替換所有 <%@ taglib ...%> 為:

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>

2. 實現登錄邏輯

package com.unmi.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

public class UserLoginAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        UserLoginForm loginForm = (UserLoginForm) form;
        String userName = loginForm.getUserName();
        String password = loginForm.getPassword();
        
        // 簡單驗證:用戶名 EricHe,密碼 123456
        if ("EricHe".equals(userName) && "123456".equals(password)) {
            request.setAttribute("userName", userName); // 供成功頁面使用
            return mapping.findForward("success");
        }
        return mapping.findForward("failure");
    }
}

Action 代碼

部署與測試

1. 添加到服務器

  • 右鍵項目 → MyEclipse → Add and Remove Project Deployments
  • Server: Tomcat 5.x → Add → Finish

部署配置

2. 啟動服務器訪問

URL: https://127.0.0.1:8080/LoginDemo/userLogin.jsp

  • 輸入用戶名: EricHe, 密碼: 123456 → 顯示成功頁面
  • 輸入錯誤或為空 → 返回登錄頁 (未實現錯誤提示)

登錄成功頁面

MyEclipse Struts Designer 核心優勢

功能傳統手寫Designer 可視化
配置文件編輯XML 手寫,易出錯圖形化拖拽,自動生成 XML
三層關聯創建分別創建 Form/Action/JSP一鍵生成關聯完整的三件套
屬性映射手動同步表單域自動綁定 Form 屬性
轉向配置手寫 <forward>可視化添加/修改
標簽庫管理手動配置 web.xml模板自動包含

遷移建議

Struts 1.x 已停止維護,新項目建議:

  • Struts 2:約定優於配置、攔截器機制、OGNL 表達式
  • Spring MVC:與 Spring 生態無縫集成、REST 支持、註解驅動
  • 現代前後端分離:Spring Boot + Vue/React,API 模式

本文僅作為維護老項目或學習 Struts 核心機制的參考。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved