我想使用java,struts2和ajax进行文件下载.
在我的html页面上有一个名为"export"的按钮点击将执行ajax调用,该调用将执行查询并将使用代码创建.xls文件,我想将该文件下载给用户而不将其存储在硬盘驱动器上.
有没有人知道如何在java中使用struts2和ajax?
有没有可用的例子?
如果您需要我的更多详细信息,请告诉我......
谢谢.
amar4kintu
我知道Struts2 Action类是线程安全的,因为操作放在Value Stack中.值栈依次是Action Context的一部分.由于Action Context是线程本地的,因此存储在Action Context(包括值栈)中的值对于每个线程都是唯一的.因此,Actions是线程安全的.
但考虑一下拦截器:它们非常有用,它们为程序员完成所有那些繁琐的小工作......比如验证,获取参数值等等.但需要考虑的是:拦截器可以在多个请求之间共享.那么这会使拦截器线程不安全吗?
考虑到这个问题,我试图在网上浏览一些与此问题相关的好文章.我发现了一篇非常好的文章,他们已经清楚地提到了一个示例拦截器如何不是线程安全的.
该网页是:http: //www.bullraider.com/java/struts2/tutorials/interceptors-and-thread-safety
我从这篇文章中得知,拦截器线程不安全的主要原因是拦截器只创建了一次.即每个拦截器只有一个对象.因此,当线程之间共享Interceptor 的相同实例时,实例字段不安全.
在文章的最后,提到有些情况,甚至拦截器都是线程安全的.但他们没有提到任何此类案件.我在网上冲浪找到答案......但是徒劳无功:(
任何人都可以告诉我或给我一个链接,在那里我可以找到如何使拦截器线程安全(或拦截器线程安全时的情况是什么)?
我在我的应用程序中使用Struts2框架,我的JSP页面上有一个按钮.那是
<s:submit type="button" name="btnSave" />
Run Code Online (Sandbox Code Playgroud)
现在,我希望此按钮的行为与正常的HTML按钮类型一样,不应提交表单并在onclick事件上执行Scripting功能.该函数使用Ajax提交表单.
但是,Struts2将其转换为
<input type="submit" id="add_btnSave" name="btnSave" value="Save"/>
Run Code Online (Sandbox Code Playgroud)
我的表格已提交.
1)如果我使用HTML按钮标签,它将弄乱GUI.我的形式的主题是Ajax.
这是一个head带脚本的标签
<head>
<s:head theme="ajax"/>
<script type="text/javascript">
$("btnSave").click(function(){
alert("aaa");
$.ajax({
url:
type:"POST",
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error ' + textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
},
success: function(){
alert('SUCCESS');
}
});
});
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
我的body标签是粉丝:
<body>
<table border="1" width="80%" align="center">
<tr>
<td width="100%">
<s:tabbedPanel id="EmpDetail" useSelectedTabCookie="true">
<s:div id="one" label="Emp Reg." theme="ajax" tabindex="0" labelposition="top">
<center>
<s:form name="frmEmpReg" namespace="/" method="post">
EMPLOYEE REGISTRATIOM TAB<br>
<s:actionmessage …Run Code Online (Sandbox Code Playgroud) 我有以下问题:当我完成一个表单并且操作保存表单的值时,我需要传递一个参数(例如ID),这将转发到结果="成功",我需要将要调用的操作在成功中带有ID和其他参数,以便以后用于保存此信息(info-form2和info.form1)...
例如:
FORM1(USER)===="成功"====> FORM2(ADDRESS)
userForm.html ===================> addressForm.html?user_id = X ...(其中X:Id将UserAction抛出(方法:保存)传递给AddressAction(方法:newAddress))
我将非常感谢你的帮助
提前致谢
是否为struts 2动作类提供了可以在该动作类的每个方法之前调用的init方法?
例如,我有一个struts 2的动作类,如下所示
import com.opensymphony.xwork2.ActionSupport;
public class EmployeeAction extends ActionSupport{
private DepartmentDaoService deptService = new DepartmentDaoService() ;
private EmployeeDaoService empService = new EmployeeDaoService();
private Employee employee;
private List<Employee> employees;
private List<Department> departments;
public void init()
{
//Do initialization stuff here
}
public String getAllEmployees(){
employees = empService.getAllEmployees();
return "success";
}
public String deleteEmployee(){
empService.deleteEmployee(employee.getEmployeeId());
return "success";
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在上面的代码时Struts动作getAllEmployees()和deleteEmplyee()被称为我想init()方法首先执行.我们可以通过从两个函数调用它来运行它.
但是struts 2中是否有任何规定会在每次调用时自动运行init方法,或者struts 2为action clases提供任何此类方法?
如果有人知道,请告诉我.
谢谢.
似乎没有一种简单的方法可以在现有的struts2-tiles插件中使用带有Struts2的Tiles 2.1或2.2.我需要在我的项目中使用tiles通配符功能,其中tiles.xml变得不可维护.
有没有办法用struts2设置图块2.1或2.2?怎么样?
谢谢,
博佐
我无法相信Struts 2中这么简单的事情看起来很难.
这几乎就像我想用Java做的那样.
for (Parent parent : parents){
for (Child child: parent.getChildren()){
System.out.println(child.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
这应该转换为Stuts标签中接近这个的东西:
<s:iterator var="parent" value="parents">
<s:iterator var="child" value="parent.children">
<s:property value="child.name"/>
<s:iterator>
<s:iterator>
Run Code Online (Sandbox Code Playgroud)
我假设parent.children应该是$ {%(#parent.children)},但我没有找到$ {%的正确组合(#字符使用:-).我还可以使用指向页面的链接来解释何时使用其中的哪一个.
我正在尝试使用s:iterator迭代一个地图列表.我可以毫无问题地遍历List,但是我不能让它迭代遍历地图的条目.到目前为止,我有这个:
[..]
<s:iterator value="records" status="recordsStatus" var="record">
<s:if test="#recordsStatus.index ==0">
<tr>
<td colspan="*"></td>
</tr>
</s:if>
<tr>
<s:iterator value="record.entrySet()" status="fieldStatus">
<td>
<s:property value="key"/>/<s:property value="value"/>
</td>
</s:iterator>
</tr>
</s:iterator>
[..]
Run Code Online (Sandbox Code Playgroud)
标签生成
<tr></tr>
Run Code Online (Sandbox Code Playgroud)
对于每个条目,但它不会通过第二个迭代器,所以我想我在使用value属性做错了.你能帮帮我吗?
谢谢
何塞
美好的一天!
我正在阅读Manning的struts2书,其中一个主题是使用语法使用OGNL访问静态变量 @[fullClassName]@[property or methodCall]
所以我在我的程序上尝试了它,我的代码如下:
豆:
public class ContactsBean {
private static int count = 1;
//getter and setter
}
Run Code Online (Sandbox Code Playgroud)
行动:
private ContactsBean contacts;
//getters and setters
Run Code Online (Sandbox Code Playgroud)
JSP:
<s:property value="@com.demo.bean.ContactsBean@count" />
or
<s:property value="@vs@count" /> //valuestack method
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我错过了什么吗?谢谢.
我们最近开始在接口中使用java 8默认方法,看起来像Freemarker看不到它们:
${myRatings.notEmpty()}
The following has evaluated to null or missing:
==> myRatings.notEmpty
Run Code Online (Sandbox Code Playgroud)
这很遗憾,因为我们在模板中调用了一堆方法.这个问题有方法解决吗?也许一些补丁?
互联网主要讲的是getFoo()默认方法,这确实没什么意义,但我说的是常规方法调用,而不是getter.
struts2 ×10
java ×6
jsp ×3
ajax ×2
iterator ×2
java-ee ×2
ognl ×2
download ×1
freemarker ×1
interceptor ×1
java-8 ×1
javascript ×1
map ×1
nested ×1
parameters ×1
tiles2 ×1