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

Struts 超簡單入門(二):JSP、struts-config.xml 與 web.xml 完整配置詳解

     編輯:Java 相關知識

Struts 超簡單入門(二):JSP、struts-config.xml 與 web.xml 完整配置詳解

承接第一篇(環境與目錄結構),本文給出 hello.jsp、struts-config.xml、web.xml 三大配置檔完整內容與關鍵標籤解析。

1. hello.jsp — 視圖層:Struts 標籤庫實踐

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html locale="true">
  <html:base></html:base>
  <head><title>Struts Hello</title></head>
  <body>
    <h2><bean:message key="hello.jsp.page.heading"/></h2>

    <html:errors/>

    <logic:present name="helloForm" scope="request">
      <h2>Hello <bean:write name="helloForm" property="person"/>!</h2>
    </logic:present>

    <html:form focus="person" action="helloWorld.do">
      <bean:message key="hello.jsp.prompt.person"/>:
      <html:text property="person" maxlength="20" size="20"/><br>
      <html:submit property="submit" value="提交"/>
      <html:reset/>
    </html:form><br>

    <html:img page="/struts-power.jpg" alt="Powered by Struts"/>
  </body>
</html:html>

標籤庫對照:

  • bean:message — 從資源檔讀取國際化文字。
  • html:form、html:text、html:submit — 生成表單元素,自動綁定 ActionForm 屬性。
  • html:errors — 顯示 ActionErrors 集合。
  • logic:present — 判斷屬性存在才渲染區塊。

2. struts-config.xml — 控制器配置核心

<struts-config>
  <form-beans>
    <form-bean name="helloForm" type="com.javer.test.struts.HelloForm"/>
  </form-beans>

  <action-mappings>
    <action path="/helloWorld"
            type="com.javer.test.struts.HelloAction"
            name="helloForm"
            scope="request"
            validate="true"
            input="/hello.jsp">
      <forward name="SayHello" path="/hello.jsp"/>
    </action>
  </action-mappings>

  <message-resources parameter="com.javer.test.struts.Application"/>
</struts-config>

關鍵屬性:

  • path="/helloWorld" 對應表單 action="helloWorld.do"(.do 由 web.xml 映射)。
  • validate="true" 啟用 HelloForm.validate()。
  • input="/hello.jsp" 驗證失敗返回頁面。
  • forward name="SayHello" 對應 HelloAction 中 mapping.findForward("SayHello")。

3. web.xml — 部署描述符:Filter、Servlet、Taglib 全配置

<web-app>
  <!-- 編碼過濾器 -->
  <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>com.javer.test.struts.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>ignore</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- ActionServlet 核心控制器 -->
  <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>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- 標籤庫聲明(Struts 1.1+ 可自動從 JAR 讀取,此處為兼容寫法) -->
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/lib/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/lib/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/lib/struts-logic.tld</taglib-location>
  </taglib>
</web-app>

配置關聯圖解

hello.jsp (action="helloWorld.do")
       │
       ▼
web.xml: *.do → ActionServlet
       │
       ▼
struts-config.xml: /helloWorld → HelloAction (form=helloForm)
       │
       ▼
HelloAction.execute() → forward "SayHello" → hello.jsp

常見配置錯誤自查

現象可能原因
404 /helloWorld.doweb.xml url-pattern 未設 *.do 或 ActionServlet 未載入
驗證不生效struts-config.xml action 未設 validate="true" 或 form-bean 名稱不匹配
中文亂碼EncodingFilter 未配置或 JSP pageEncoding 不一致
標籤庫報錯taglib uri/location 路徑錯誤,Struts 1.2+ 建議移除 web.xml taglib 聲明,依賴 JAR 內 META-INF/tld

三大配置檔搞定,Struts 控制器骨架即成型。下一篇將給出 Action、Form、Model 完整 Java 代碼,系列完結。

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