当前使用的版本是2.3.16.3,点击下载http://struts.apache.org/
struts2推荐使用通配符的方式配置,下面的是详细的使用方法:
第一:新建一个web项目,导入里面的相关jar包,并配置web.xml,配置如下。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>tea</display-name> <!-- 配置欢迎页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <!-- 从struts2开始,采用了StrutsPrepareAndExecuteFilter这个类来作为拦截器入口 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern><!-- 这里为了方便查询是生效了structs2,配置了拦截所有的页面 --> </filter-mapping> </web-app>
第二:
新建一个测试调用动态方法的页面。比如index.jsp或者是index.html,文件名 自定义,但是结尾是.jsp或者是.html结尾。下面以index.jsp为例,
在index.jsp页面中放置4个超链接。只要是看点击哪个连接,它调用了Action里面的哪个方法。(一般最后是放3个以上(含),就可以总结出里面的方法是怎么调用的了。),页面代码如下:
<%@ 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>TestMethod</title> </head> <body> <a href="addDiplay">测试方法1</a> <a href="goodDiplay">测试方法2</a> <a href="delDiplay">测试方法3</a> <a href="serDiplay">测试方法4</a> </body> </html>
第三:新建一个action.Action里面如下:
package com.ye; import com.opensymphony.xwork2.ActionSupport; public class Show extends ActionSupport{ private static final long serialVersionUID = 6724224670598786797L; public String add(){ return "one"; //如果引用的是这个跳转到one.jsp } public String good(){ return "two";//如果引用的是这个跳转到two.jsp } public String del(){ return "three";//如果引用的是这个跳转到three.jsp } public String ser(){ return "four";//如果引用的是这个跳转到four.jsp } }
第四,新建四个页面,只要是跟action里面的方法返回对应的字符串时,跳转到对应的方法里面的页面。代码如下:
one的页面,也就是one.jsp(这里html,jsp页面都可以)l如下:
<%@ 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> 我是第一个页面哦 </body> </html>
其他三个页面与这个类似,只是改一下<title>和<body>里面的文字而己。
第五:创建struts.xml文件,直接放在项目里面的src目录里面即可:
对于struts.xml的动态方法配置说明如下:
============================================================================
说明1:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="display" extends="struts-default"> <!-- <a href="addDiplay">测试方法1</a> <a href="goodDiplay">测试方法2</a> <a href="delDiplay">测试方法3</a> <a href="serDiplay">测试方法4</a> --> <!--这里Action里面匹配的是Diplay--> <action name="*Diplay" class="com.ye.Show" method="{1}"> <result name="one">/one.jsp</result> <result name="two">/two.jsp</result> <result name="three">/three.jsp</result> <result name="four">/four.jsp</result> </action> </package> </struts>
这里采取*Diplay。当点击index.jsp里面的超连接时(比如点击测试1),它会匹配里面的对应的方法。比如add()这个。*号表示任意匹配,它会直接匹配*号里面的内容,Action里面如果有相应的方法匹配,就执行对应的方法。
==============================================================================
说明2:当我们修改struts.xml里面验证匹配的方式如下时:(比如匹配规则为*lay):
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="display" extends="struts-default"> <!-- <a href="addDiplay">测试方法1</a> <a href="goodDiplay">测试方法2</a> <a href="delDiplay">测试方法3</a> <a href="serDiplay">测试方法4</a> --> <action name="*lay" class="com.ye.Show" method="{1}"> <result name="one">/one.jsp</result> <result name="two">/two.jsp</result> <result name="three">/three.jsp</result> <result name="four">/four.jsp</result> </action> </package> </struts>
它会报出如下的错误:
HTTP Status 500 - com.ye.Show.addDip() type Exception report message com.ye.Show.addDip() description The server encountered an internal error that prevented it from fulfilling this request. exception java.lang.NoSuchMethodException: com.ye.Show.addDip() java.lang.Class.getMethod(Class.java:1670) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.getActionMethod(AnnotationValidationInterceptor.java:75) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:47) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468) org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.57 logs. Apache Tomcat/7.0.57
其中最关键的是这种:java.lang.NoSuchMethodException: com.ye.Show.addDip(),也说是exception下面的这个,它的意思是说方法没有找到。(见附件1);
所以,对于struts的通配符配方式。它是匹配:按先找到匹配规则。不看* 号那部分。比如上面的 Diplay 和 lay 找到之后,就去匹配 * 号里面的内容。如果匹配成功就执行对应的方法。
下面还是以测试连接为例。
看匹配规则1 : *Diplay这个。它依据 Diplay 这个规则去匹配。之后再去匹配*号,当我们点击对应的链接,比如连接1时,也就是这个<a href="addDiplay">测试方法1</a>,它匹配成功。找到了Action里面的add()这个方法。所以它就执行,之后跳转到one.jsp这个页面。
看匹配规则 2 : *lay这个。它先依据 lay 这个规则找到对应Action里面的方法。之后去找 * 号里面对应的方法。 当单击<a href="addDiplay">测试方法1</a>等这些连接时。它匹配的方法就是 addDi等这些方法。因为没有找到,所以它就报出了这个没有方法的异常了。
所以综合上面的。它通配符的方式后面不含 * 号的它类似于一个筛选器一样。它先打到对应的匹配规则。之后看审核 * 里面的内容。如果有就匹配里面的内容和Action方法定义的一样的方法名。如果存在就执行匹配成功的方法。如果没有它就抛出一个方法没有找到的异常,也就是NoSuchMethod的这处异常信息。
附件为对应的代码。
相关推荐
struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置...
struts动态方法配置
struts2配置过滤器 示例代码
一个简单的struts2的手动配置一个简单的struts2的手动配置一个简单的struts2的手动配置
javaEE学习阶段,struts2初步使用环境配置。 javaEE学习阶段,struts2初步使用环境配置。
struts2 在eclipse 配置成功
struts2 Https , 轻松开发基于struts2的https开发
Struts2配置详解,Struts2配置详解,Struts2配置详解,Struts2配置详解,Struts2配置详解
Struts 2使用注解配置Action,不配置struts.xml,通过注解直接配置action
struts2 配置dtd 自动提示 struts2 配置dtd 自动提示
关于struts2 Result的配置,有例子
STRUTS2:零配置插件CodeBehind
struts2核心配置文件,struts2.xml和struts2.properties
·Struts2配置文件介绍 struts.xml action package ·Struts2配置文件介绍 struts.xml action package
如果设置为true,Struts2将允许动态方法调用。 11. struts.freemarker.manager.classname:这是一个FreemarkerManager实现类,Struts2用于处理FreeMarker模板。 12. struts.i18n.encoding:这是国际化信息的内码,...
配置Struts配置Struts配置Struts配置Struts配置Struts配置Struts
讲述了配置struts2的三种方法:struts.xml struts.properties web.xml
struts2的lib资源 struts2的基本配置
Freemarker是模板引擎,也可以说是一种...开发人员只要熟悉Struts2的标签即可进行开发Freemarker模板文件,如果您还不熟悉具体的Struts2及标签应用请参考其他文献资料,本文主要阐述Struts2与Freemarker的配置方法。。