Struts2 页面url请求怎样找action
2016-03-07 17:52
716 查看
1.我们使用最原始的方法去查找action。不同注解。
struts.xml文件先配置
<!-- 新闻信息action -->
<action name="newsInfoAction" class="com.xxx.NewsInfoAction">
<result name="add">news/addNewsInfo.jsp</result>
<result name="update">news/editNewsInfo.jsp</result>
<result name="dataList">news/newsInfo.jsp</result>
</action>
action 默认运行的是NewsInfoAction中的excute方法。
http://localhost:8080/test/newsInfoAction.html 或者http://localhost:8080/test/newsInfoAction.action 看你怎样在struts.xml文件里的配置(
<!-- 改动后缀 -->
<constant name="struts.action.extension" value="action,html" /> )
那么有一个疑问,我们怎么訪问NewsInfoAction中的其它方法呢?
訪问指定方法
方式一:
http://localhost/struts2/simple/hello!say.action
能够调用hello这个action中的say方法
方式二:
http://localhost/struts2/simple/hello.action?method:say=xxx
能够调用say方法。在这里,參数的名称是:method:say,这是最基本的,struts2正是
依据參数的名称来决定该调用哪个方法,而不是參数的值,所以參数的值能够是随意的
方式三:
struts2的配置文件的action标签中存在一个method属性,用来指定訪问特定的方法
<action name="hello" class="helloAction" method="say">
方式四:
<action name="hello_*" class="helloAction" method="{1}">
这样在页面中的action路径可写为hello_say.action就是訪问say方法了。
2.假设struts2已经交给spring容器管理了。我们就能够通过注解来找action以及该action的方法了。
(推荐使用这样的方法。这样我们你就不用在struts.xml文件里再去配置各种action,能够给struts.xml减肥啦。
)
url为 :http://localhost:8080/test/admin/editproduct.html?productInfoId=1 //參数可有可无
@SuppressWarnings("unchecked")
@Action(value = "/admin/editproduct", results = { @Result(name = "update", location = "product/editProductInfo.jsp") })
public String toUpdateProductInfo() throws Exception {
。
。
。。。。。
}
配置文件仅仅须要配置注解就可以:
<mvc:annotation-driven />
<context:annotation-config></context:annotation-config>
不能简写成<context:annotation-config/>
struts.xml文件先配置
<!-- 新闻信息action -->
<action name="newsInfoAction" class="com.xxx.NewsInfoAction">
<result name="add">news/addNewsInfo.jsp</result>
<result name="update">news/editNewsInfo.jsp</result>
<result name="dataList">news/newsInfo.jsp</result>
</action>
action 默认运行的是NewsInfoAction中的excute方法。
http://localhost:8080/test/newsInfoAction.html 或者http://localhost:8080/test/newsInfoAction.action 看你怎样在struts.xml文件里的配置(
<!-- 改动后缀 -->
<constant name="struts.action.extension" value="action,html" /> )
那么有一个疑问,我们怎么訪问NewsInfoAction中的其它方法呢?
訪问指定方法
方式一:
http://localhost/struts2/simple/hello!say.action
能够调用hello这个action中的say方法
方式二:
http://localhost/struts2/simple/hello.action?method:say=xxx
能够调用say方法。在这里,參数的名称是:method:say,这是最基本的,struts2正是
依据參数的名称来决定该调用哪个方法,而不是參数的值,所以參数的值能够是随意的
方式三:
struts2的配置文件的action标签中存在一个method属性,用来指定訪问特定的方法
<action name="hello" class="helloAction" method="say">
方式四:
<action name="hello_*" class="helloAction" method="{1}">
这样在页面中的action路径可写为hello_say.action就是訪问say方法了。
2.假设struts2已经交给spring容器管理了。我们就能够通过注解来找action以及该action的方法了。
(推荐使用这样的方法。这样我们你就不用在struts.xml文件里再去配置各种action,能够给struts.xml减肥啦。
)
url为 :http://localhost:8080/test/admin/editproduct.html?productInfoId=1 //參数可有可无
@SuppressWarnings("unchecked")
@Action(value = "/admin/editproduct", results = { @Result(name = "update", location = "product/editProductInfo.jsp") })
public String toUpdateProductInfo() throws Exception {
。
。
。。。。。
}
配置文件仅仅须要配置注解就可以:
<mvc:annotation-driven />
<context:annotation-config></context:annotation-config>
不能简写成<context:annotation-config/>
相关文章推荐
- java只有值传递
- Dubbo admin在JDK1.8环境运行报Invalid property 'URIType' of bean class
- JAVA Scanner的用法
- java 笔试易错题(二 )
- ViewPager报java.lang.IllegalStateException,without calling PagerAdapter#notifyDataSetChanged!
- Java虚拟机有两种类装载器
- JDK安装和配置
- java用三种方法实现阶乘n!
- Java学习心得
- Java之Base64实现文件和字符串之间的转换
- java annotation注解学习,自定义注解
- Java的Executors(线程池)使用
- java Socket用法详解
- java设计模式学习笔记第四章
- JavaSE_Java反射机制详解(Field,Method) 及 Method.invoke使用方法
- Java——jar命令
- Spring之IOC学习
- leetcode:Path Sum 【Java】
- java加密解密___MD5的简单使用
- Java IO流详尽解析