歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2學習筆記之開發環境搭建

Struts2學習筆記之開發環境搭建

日期:2017/3/1 10:39:47   编辑:Linux編程

對於struts2現在用的是相當的多啊,,不學看來就得落伍了,記下一系列的學習筆記,供以後復習使用。

首先還是從所有程序員都認識的HelloWorld開始。

1、加入必要的開發包,這裡所列的包是從struts2自帶的demo中拷貝出來的。有如下的一些。




2、配置web.xml:

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <filter>
  11. <filter-name>struts2</filter-name>
  12. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>struts2</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. </web-app>
3、編寫進行邏輯處理的Action

Action可以是普通的POJO、還可以是繼承ActionSupport的類、或是實現Action的類,對於繼承或實現的方式,裡面會有一些常量,方便作為返回值使用

[java]

  1. Action.ERROR ;
  2. Action.INPUT ;
  3. Action.LOGIN ;
  4. Action.NONE ;
  5. Action.SUCCESS ;
編寫一個簡單的類實現ActionSupport

[java]

  1. package com.akwolf.helloworld.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class LoginAction extends ActionSupport {
  4. private static final long serialVersionUID = 1L;
  5. private String username;
  6. private String password;
  7. @Override
  8. public String execute() throws Exception {
  9. if ("akwolf".equals(username) && "123456".equals(password)) {
  10. return "success";
  11. }
  12. return "error";
  13. }
  14. public String getUsername() {
  15. return username;
  16. }
  17. public void setUsername(String username) {
  18. this.username = username;
  19. }
  20. public String getPassword() {
  21. return password;
  22. }
  23. public void setPassword(String password) {
  24. this.password = password;
  25. }
  26. }

4、進行struts.xml的配置

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <!-- 包機制防止重名,action也必須放在package下 ,可以繼承自一個包-->
  7. <package name="helloworld" extends="struts-default" namespace="/">
  8. <!-- 具體的用來請求處理邏輯的action,並指定使用哪一個action進行處理 -->
  9. <action name="login" class="com.akwolf.helloworld.action.LoginAction">
  10. <!-- action處理後的返回結果,根據結果進行相應的視圖跳轉 -->
  11. <result name="success">/welcom.html</result>
  12. <result name="error">/error.html</result>
  13. </action>
  14. </package>
  15. </struts>
5、編寫三個簡單頁面,進行視圖層的顯示。

index.jsp

[html]

  1. <body>
  2. <form action="login.action" method="post">
  3. <table align="center">
  4. <caption><h3>用戶登錄</h3></caption>
  5. <tr>
  6. <td>
  7. <span style="white-space:pre"> </span>用戶名:<input type="text" name="username" />
  8. </td>
  9. </tr>
  10. <tr>
  11. <td>
  12. <span style="white-space:pre"> </span>密碼:<input type="text" name="password"/>
  13. </td>
  14. </tr>
  15. <tr align="center">
  16. <td>
  17. <span style="white-space:pre"> </span><input type="submit" value="登錄"/>
  18. <span style="white-space:pre"> </span><input type="reset" value="重置"/>
  19. </td>
  20. </tr>
  21. </table>
  22. </form>
  23. </body>
welocm.html

[html]

  1. <body>
  2. <h3>歡迎方位<a href="index.jsp">返回</a></h3>
  3. </body>

error.html

[html]

  1. <body>
  2. <h3>信息錯誤!!<a href="index.jsp">返回</a></h3>
  3. </body>
ok,部署運行,第一個struts程序搞到!!!!
Copyright © Linux教程網 All Rights Reserved