struts中form表單中的請求action路徑和struts-config.xml中響應路徑的注意點

來源:互聯網
上載者:User

舉個例子:本人建立了一個ssh的項目demo,項目結構如下圖所示:


這個項目有登陸介面,有login.jsp檔案,這個檔案的form表單請求如下

<body>    <h1>管理員登入</h1>    <form action="/ssh/login.do?flag=login" method="post">    id:<input type="text" name="id"/><br/>    pw:<input type="password" name="pwd"><br/>    <input type="submit" value="登陸"/>    </form>  </body>

這裡form表單還可以這樣寫

<body>    <h1>管理員登入</h1>    <form action="login.do?flag=login" method="post">    id:<input type="text" name="id"/><br/>    pw:<input type="password" name="pwd"><br/>    <input type="submit" value="登陸"/>    </form>  </body>

struts-config.xml中配置響應action路徑如下

<struts-config><form-beans><form-bean name="employeeForm" type="com.gt.web.forms.EmployeeForm"></form-bean></form-beans><action-mappings><action path="/login" parameter="flag" name="employeeForm" type="com.gt.web.action.LoginAction"><forward name="ok" path="/WEB-INF/mainFrame.jsp"></forward><forward name="error" path="/WEB-INF/login.jsp"></forward></action></action-mappings></struts-config>
這裡我們發現在表單中可以使用第一種url路徑方式,這種寫法是絕對路徑的寫法;第二種寫法則是相對路徑,不需要寫斜杠,系統預設是該項目下,在這個項目中就是  /ssh ("ssh"為該項目名)下面。

還有第三種路徑的寫法,這個寫法是採用request.getContextPath()來擷取項目根路徑

 <body>    <h1>管理員登入</h1>    <form action="<%=request.getContextPath()%>/login.do?flag=login" method="post">    id:<input type="text" name="id"/><br/>    pw:<input type="password" name="pwd"><br/>    <input type="submit" value="登陸"/>    </form>  </body>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.