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在命名空间的后边还是要用类来区分一下。
- Struts中1个Action中多个执行方法时,怎么调用
- Struts框架 action怎么动态调用方法
- Struts同一个action执行多个方法
- Struts action 多个方法
- Struts 2中如何用一个action调用多个方法,避免每一个方法写一个action的繁琐之处
- Struts返回Json 格式result时action中方法调用两次
- Struts:@Action注解方法中如何调用另一个@Action注解的方法
- struts的Action接口-- (1)动态方法调用
- 【Struts】Action 3 动态方法调用
- struts之Action的调用方法
- struts一个action处理多个方法
- struts.xml中配置一个action调用另一个action
- action中调用另一个action中的方法
- 在struts中的Action方法中,调用request.getInputStream() 发现无法读到任何数据
- struts2 Action中方法不要以get和set开头,否则会被struts调用
- 关于struts如何在action中实现多个处理方法
- struts中action的修改方法
- 一个action多个方法的调用
- java 泛型嵌套
- sencha touch权威指南---学习笔记4-插入图片单击事件
- 【SDOI2013】保护出题人
- Eclipse上安装GIT插件EGit及使用
- 手势识别器实现界面切换
- Struts中1个Action中多个执行方法时,怎么调用
- Matrix Swapping II - HDU 2830 dp
- 解决action与iframe显示问题
- 黑马程序员---面向对象核心思想总结
- 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
- Uva 11137 Ingenuous Cubrency(DP)
- mysql update修改多条数据
- [JavaScript]-----正则表达式
- 职场感悟