---
title: "Struts2 页面url请求如何找action"
author: "mywebcode"
date: "2014-04-29T15:01:00Z"
tags: []
slug: "struts2-url-action"
draft: false
lastmod: "2014-04-30T09:15:43Z"
id: 2058290
url: "https://mywebcode.iteye.com/blog/2058290"
---

1.我们使用最原始的方法去查找action，不同注解。

struts.xml文件先配置

&lt;!-- 新闻信息action --&gt;  
&lt;action name="newsInfoAction" class="com.xxx.NewsInfoAction"&gt;  
&lt;result name="add"&gt;news/addNewsInfo.jsp&lt;/result&gt;  
&lt;result name="update"&gt;news/editNewsInfo.jsp&lt;/result&gt;  
&lt;result name="dataList"&gt;news/newsInfo.jsp&lt;/result&gt;  
&lt;/action&gt;

action 默认执行的是NewsInfoAction中的excute方法。 http://localhost:8080/test/newsInfoAction.html 或者http://localhost:8080/test/newsInfoAction.action 看你如何在struts.xml文件中的配置（

&lt;!-- 修改后缀 --&gt;  
&lt;constant name="struts.action.extension" value="action,html" /&gt; ）

那么有一个疑问，我们怎么访问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属性，用来指定访问特定的方法

&lt;action name="hello" class="helloAction" method="say"&gt;

**方式四：**

&lt;action name="hello\_\*" class="helloAction" method="{1}"&gt;

这样在页面中的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](http://localhost:8080/mc/admin/editproduct.html?productInfoId=1) //参数可有可无

@SuppressWarnings("unchecked")  
@Action(value = "/admin/editproduct", results = { @Result(name = "update", location = "product/editProductInfo.jsp") })  
public String toUpdateProductInfo() throws Exception {

。。。。。。。

}

配置文件只需要配置注解即可：

&lt;mvc:annotation-driven /&gt;  
&lt;context:annotation-config&gt;&lt;/context:annotation-config&gt; 不能简写成&lt;context:annotation-config/&gt;