struts2静态方法和动态方法调用
生活随笔
收集整理的這篇文章主要介紹了
struts2静态方法和动态方法调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"> <title>struts2靜態方法和動態方法調用</title></head><body><a href="${pageContext.request.contextPath }/methodLogin.a">通過靜態方法調用進入login</a><br /><a href="${pageContext.request.contextPath }/methodLoginA.b">通過靜態方法調用進入loginA</a><br /><a href="${pageContext.request.contextPath }/methodDync!login.action">通過動態方法調用進入login</a><br /><a href="${pageContext.request.contextPath }/methodDync!loginA.action">通過動態方法調用進入loginA</a><br /><a href="${pageContext.request.contextPath }/methodDync!loginB.action">通過動態方法調用進入loginB</a><br /></body> </html>2 method.xml頁面(必須在struts2.xml里定義,定義的格式為:<include file="method.xml"></include>)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="methodPackage" extends="struts-default"><!-- 定義了兩個靜態方法 method是指定跳轉到類的某個自定義方法--> <action name="methodLogin" class="cn.etop.struts2.method.MethodAction" method="login"><result name="test">/method/success.jsp</result></action><action name="methodLoginA" class="cn.etop.struts2.method.MethodAction" method="loginA"><result name="test">/method/success.jsp</result></action><!-- 定義了一動態方法,但在jsp請求路徑中,必須在類名后加“!要執行的方法名”如:action的name為:login,動態執行的方法為test,那么jsp的路徑為:login!test.action開啟動態方法有三種方式第一種:在web.xml里配置參數<init-param><param-name>struts.enable.DynamicMethodInvocation</param-name><param-value>true</param-value></init-param> 第二種:在src下創建一個屬性文件:struts.properties(名字struts是固定的,不能是別的名字) struts.enable.DynamicMethodInvocation = true第三種:在struts.xml里配置參數<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> --><action name="methodDync" class="cn.etop.struts2.method.MethodAction" ><result name="test">/method/success.jsp</result></action></package></struts>3 java代碼
package cn.etop.struts2.method;import java.io.IOException;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** 測試靜態方法和動態方法* 自定義從xml請求的路徑執行的方法* * @author Administrator**/ public class MethodAction extends ActionSupport {public String login(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法login");} catch (IOException e) {e.printStackTrace();}return null; }public String loginA(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法loginA");} catch (IOException e) {e.printStackTrace();}return null; }public String loginB(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法loginB");} catch (IOException e) {e.printStackTrace();}return null; }public String loginC(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法loginC");} catch (IOException e) {e.printStackTrace();}return null; } }4 在web.xml里配置動態方法
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 定義Struts 2的FilterDispatcher的Filter --><filter><!-- 定義核心Filter的名字 --><filter-name>struts2</filter-name><!-- 定義核心Filter的實現類 --><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- 配置動態方法 --><init-param><param-name>struts.enable.DynamicMethodInvocation</param-name><param-value>true</param-value></init-param></filter><!-- FilterDispatcher用來初始化Struts 2并且處理所有的Web請求 --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>5 在struts2.properties里配置動態方法
#設置是否啟動動態方法 struts.enable.DynamicMethodInvocation = true #設置struts2的后綴 struts.action.extension=action,a,b #設置是否啟動開發模式 struts.devMode = true #設置國際化屬性文件的basename struts.custom.i18n.resources=message #設置上傳文件的大小 struts.multipart.maxSize=209715206 在struts2.xml配置動態方法
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <!-- 表示自動掃描包結構中包含配置的字符 并帶有注解的action--><constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/><!-- 表示自動掃描以某個后綴結尾的類 要求必須帶有注解 --><constant name="struts.convention.action.suffix" value="Action"/><!-- 表示從哪個包開始掃描 --><constant name="struts.convention.package.locators.basePackage" value="cn.et"/><!-- include是其他的xml必須在Struts里定義,相當于繼承 --> <include file="method.xml"></include> </struts>轉載于:https://www.cnblogs.com/t0404/p/10290981.html
總結
以上是生活随笔為你收集整理的struts2静态方法和动态方法调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django jquery ajax
- 下一篇: 非常复杂,上双11数据大屏背后的秘密:大