Struts中1个Action中多个执行方法时,怎么调用

来源:互联网 发布:如何手机注销淘宝店铺 编辑:程序博客网 时间:2024/05/16 19:07

先看工程目录:

最近遇到一个问题,总是访问不到,404错误,提示找不到Action

                                                                     



部分目录可以不用,说明问题即可: 


<%@ 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>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">    <script type="text/javascript" src="jquery-1.11.1.min.js"></script>    <script type="text/javascript" >          $(function(){        $('#search').click(function(){          var _name=$(':input[name]').val();          $.post('<%=basePath%>demo/list?x='+_name+'&t='+new Date().getTime(),function(data){              var _html="<table><tr><th>aaa</th><th>bbb</th><th>ccc</th></tr>";              var _tr="";              $.each(data.list,function(i,e){                _tr+="<tr><td>"+e.x+"</td><td>"+e.y+"</td><td>"+e.z+"</td></tr>";              });              var _table=_html+_tr+"</table>";              $('#userlist').html(_table);              $('table').css('border','1px').width(500);          });                });      });        </script>  </head>    <body>    <input type="text" name="name"/><button id="search">Search</button>    <hr/>    <div id="userlist"></div>      </body></html>


下面的配置相当重要:含有Action包的所有包

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>    <!--确定搜索包的路径。只要是结尾为action的包都要搜索。-->     <constant name="struts.convention.package.locators" value="action" />  </struts>    



下面是Action中的几个方法:

package hglq4.cn.eshop.action.demo;import hglq4.cn.eshop.vo.demo.DemoVO;import java.util.ArrayList;import java.util.List;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")//@ParentPackage("json-default")@ParentPackage("struts-default")@Results({@Result(name="success",location="/index.jsp")})public class DemoAction extends ActionSupport {private String x;private List<DemoVO> list=new  ArrayList<DemoVO>();public List<DemoVO> getList() {return list;}public String getX() {return x;}public void setX(String x) {this.x = x;}@Action(value="add",results={@Result(name="success",location="/index.jsp")})public String add(){System.out.println("-------5678-----------");return SUCCESS;}public String del(){System.out.println("---------2-----------");return SUCCESS;}//@Action(value="list",results={@Result(name="success",type="json")})public String list(){System.out.println("---------list-----------"+x);/*if(x.equals("a")){for(int i=0;i<10;i++){  DemoVO d=new DemoVO("1","2",i+++"");  list.add(d);}}else{for(int i=20;i<80;i++){  DemoVO d=new DemoVO("ad","ef",i+++"");  list.add(d);}}*/return SUCCESS;}}



那么,访问的方式分别为:

http://127.0.0.1:8080/pdemo/demo/add

http://127.0.0.1:8080/pdemo/demo/demo!del

http://127.0.0.1:8080/pdemo/demo/demo!list

解释,因为配置了:

<struts>    <!--确定搜索包的路径。只要是结尾为action的包都要搜索。-->     <constant name="struts.convention.package.locators" value="action" />  </struts>    


(其实这里有一个规范:包名第一个单词必须是Action,类名必须以Action结尾。如图:

                                                      

那么就扫描含有Action包下的所有包极其子类,访问的时候去掉Action后的所有包的类:

                              1.对于add方法,因为已经注解,所以直接在包demo下写方法:demo/add

                               2.对于del与list,跟前面add方法差不多,对于包去掉Action,对于类也去掉action,并且全部小写,然后加方法名。




如果说加了命名空间:

                                                                      

                                        

那么访问的的路径是这样的:

http://127.0.0.1:8080/pdemo/s/demo!delhttp://127.0.0.1:8080/pdemo/s/demo!listhttp://127.0.0.1:8080/pdemo/s/add

指定命名空间的意思就是说:我就是命名空间,你们按照的来,那么就不用参考包了,对于已经配好的方法,直接在url的命名空间后加方法即可:s/add

对于没有配注解的方法:那么,还是要参考类来访问,如下:

http://127.0.0.1:8080/pdemo/s/demo!delhttp://127.0.0.1:8080/pdemo/s/demo!list在命名空间的后边还是要用类来区分一下。





0 0
原创粉丝点击