2017年7月24日 星期一

Struts2 DMI 方法使用


Server Tomcat : 8.5
JDK version:8
Struts2 Version:2.5.12

1.Action Class設定

 package org.homenet.mituo.user.action;  
 import java.util.Date;  
 import com.opensymphony.xwork2.ActionSupport;  
 public class UserAction extends ActionSupport{  
      private static final long serialVersionUID = 1L;  
      public String add() {  
           System.out.println("Do Add User Working ..." + new Date());  
           return "addSuccess";  
      }  
      public String del() {  
           System.out.println("Do Delete User Working ..." + new Date());  
           return "delSuccess";  
      }  
 }  

2.web.xml設定

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  
  <display-name>Strtus2_0500_Introduction</display-name>  
  <welcome-file-list>  
   <welcome-file>index.html</welcome-file>  
   <welcome-file>index.htm</welcome-file>  
   <welcome-file>index.jsp</welcome-file>  
   <welcome-file>default.html</welcome-file>  
   <welcome-file>default.htm</welcome-file>  
   <welcome-file>default.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
   <filter-name>action2</filter-name>  
   <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
  <filter-mapping>  
   <filter-name>action2</filter-name>  
   <url-pattern>/*</url-pattern>  
  </filter-mapping>  
 </web-app>  

3.struts.xml設定

 <?xml version="1.0" encoding="UTF-8" ?>  
 <!DOCTYPE struts PUBLIC  
      "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"  
      "http://struts.apache.org/dtds/struts-2.5.dtd">  
 <!-- START SNIPPET: xworkSample -->  
 <struts>  
       <!-- 預設DMI方法是關閉,須手動開啟,並設定允許執行方法allowed-methods -->  
   <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
       <constant name="struts.devMode" value="true" />  
       <package name="front" extends="struts-default" namespace="/user" strict-method-invocation="true">  
      <action name="user" class="org.homenet.mituo.user.action.UserAction">  
           <result name="addSuccess">/UserAddSuccess.jsp</result>  
           <result name="delSuccess">/UserDeleteSuccess.jsp</result>  
           <allowed-methods>add,del</allowed-methods>  
      </action>  
       </package>  
 </struts>  
 <!-- END SNIPPET: xworkSample -->  

4.JSP文件

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>動態方法調用DMI</title>  
 </head>  
 <body>  
 <h3>用戶管理</h3><br>  
      <a href="user/user!add">執行新增使用者</a>  
      <br><br>  
      <a href="user/user!del">執行刪除使用者</a>  
 </body>  
 </html>  

UserAddSuccess.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>Insert title here</title>  
 </head>  
 <body>  
 User Add OK !!  
 </body>  
 </html>  

UserDeleteSuccess.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>Insert title here</title>  
 </head>  
 <body>  
 User Delete OK !!  
 </body>  
 </html>  

沒有留言:

張貼留言