1. 程式人生 > >Java struts 學習記錄與案例

Java struts 學習記錄與案例

開始進行測試,引入2個檔案:

index.jsp

程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主頁</title>
</head>
<body>
 <a href="first.action">請求Struts2</a>
</body>

</html>

在web。xml檔案中新增對映器:


   <!--  Struts2過濾器 -->
<filter>
<!--  過濾器名稱 -->
  <filter-name>struts2</filter-name>
<!--  過濾器類 -->
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
 <!--  Struts2過濾器對映 -->
 <filter-mapping>
 <!--  過濾器名稱 -->
  <filter-name>struts2</filter-name>
 <!--  過濾器對映  -->
  <url-pattern>/*</url-pattern>
  </filter-mapping>
 

檔案 struts.xml 放到專案的src目錄下, 程式碼是:

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <!-- 宣告包 -->
 <package name="myPackage" extends="struts-default">
  <!-- 定義action -->
  <action name="first">
   <!-- 定義處理成功後的對映頁面 -->
   <result>/first.jsp</result>
  </action>
 </package>
</struts>

  
  
 新增 包檔案:

開始進行測試,引入2個檔案:

index.jsp

程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主頁</title>
</head>
<body>
 <a href="first.action">請求Struts2</a>
</body>

</html>

first.jsp程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一個Struts2程式</title>
</head>
<body>
 第一個Struts2程式!
 <br>
</body>
</html>

=================================================================================

動態action的應用

index.jsp 程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首頁</title>
</head>
<body>
 <a href="userAction!add">新增使用者</a>
 <br>
 <a href="userAction!update">更新使用者</a>
</body>
</html>

--------------------------------------------------------------------

在web。xml檔案中新增對映器:


   <!--  Struts2過濾器 -->
<filter>
<!--  過濾器名稱 -->
  <filter-name>struts2</filter-name>
<!--  過濾器類 -->
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
 <!--  Struts2過濾器對映 -->
 <filter-mapping>
 <!--  過濾器名稱 -->
  <filter-name>struts2</filter-name>
 <!--  過濾器對映  -->
  <url-pattern>/*</url-pattern>
  </filter-mapping>
 

------------------------------------------------------------------

struts.xml程式碼:

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <!-- 宣告包 -->
    <package name="myPackage" extends="struts-default">
     <!-- 定義action -->
     <action name="userAction" class="com.lyq.action.UserAction">
      <!-- 新增成功的對映頁面 -->
      <result name="add">user_add.jsp</result>
      <!-- 更新成功的對映頁面 -->
      <result name="update">user_update.jsp</result>
     </action>
    </package>
</struts>

 ---------------------------------------------------------------------

UserAction.Java 類程式碼:

package com.lyq.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
 private static final long serialVersionUID = 1L;
 // 提示資訊
 private String info;
 // 新增使用者資訊
 public String add() throws Exception{
  info = "新增使用者資訊";
  return "add";
 }
 // 更新使用者資訊
 public String update() throws Exception{
  info = "更新使用者資訊";
  return "update";
 }
 public String getInfo() {
  return info;
 }
 public void setInfo(String info) {
  this.info = info;
 }
}

------------------------------------------------------------------

user_add.jsp程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增使用者資訊</title>
</head>
<body>
 <font color="red">
  <s:property value="info"/>
 </font>
</body>
</html>

-------------------------------------------------------------------

user_update.jsp檔案程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>更新使用者資訊</title>
</head>
<body>
 <font color="red">
  <s:property value="info"/>
 </font>
</body>

</html>

=========================================================================================

 處理對錶單提交的資料:

index.jsp程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主頁</title>
<style type="text/css">
 *{font-size: 12px;}
</style>
</head>
<body>
  <form action="greeting.action" method="post">
   請輸入你的姓名:<input type="text" name="username">
   <input type="submit" value="提交">
  </form>
</body>
</html>

 ----------------------------------------------------------------------

struts.xml程式碼:

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <!-- 宣告包 -->
    <package name="myPackage" extends="struts-default">
     <!-- 定義action -->
     <action name="greeting" class="com.lyq.action.GreetingAction">
      <!-- 定義成功的對映頁面 -->
      <result name="success">success.jsp</result>
      <!-- 定義失敗的對映頁面 -->
      <result name="error">error.jsp</result>
     </action>
    </package>
</struts>

 ---------------------------------------------------------------

 GreetingAction.java程式碼:

package com.lyq.action;
import com.opensymphony.xwork2.ActionSupport;

public class GreetingAction extends ActionSupport {
 private static final long serialVersionUID = 1L;
 // 使用者名稱
 private String username;
 // 處理請求
 @Override
 public String execute() throws Exception{
  // 判斷使用者名稱是否有效
 if(username == null || "".equals(username)){
   // 返回到錯誤頁面
   return ERROR;
  }else{
   // 返回到成功頁面
   return SUCCESS;
  }
 }
 // username屬性的getter方法
 public String getUsername() {
  return username;
 }
 // username屬性的setter方法
 public void setUsername(String username) {
  this.username = username;
 }
}

-------------------------------------------------------

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>歡迎</title>
<style type="text/css">
 *{font-size: 12px;}
</style>
</head>
<body>
 <font color="red">
  <s:property value="username" />
 </font>
  ,您好!
 <br>
 歡迎使用來到本站。
</body>
</html>

---------------------------------------------------------

error.jsp程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>錯誤</title>
</head>
<body>
 <font color="red"> 錯誤:您沒有輸入姓名!</font>
</body>
</html>