gpt4 book ai didi

java - 如何使用 Struts 将一个简单的字符串从 Action 获取到 JSP?

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:37 25 4
gpt4 key购买 nike

我有这个问题:我需要从 Action 获取一些简单的数据并将其发送回 JSP(已加载),因此我必须管理该操作以使用 jQuery $.ajax 方法恢复该数据。这是我的代码:

MyAction.java

private String employee;
private InputStream inputStreamEmployee;
//these with getter/setter

public String someData() throws Exception{
employee= "John Doe";
inputStreamEmployee = new ByteArrayInputStream(
employee.getBytes(StandardCharsets.UTF_8));

return "SUCCESS";
}

struts.xml

<action name="getSomeData" method="someData" class="MyAction">
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStreamEmployee</param>
</result>
</action>

data.js

function getData(){
$.ajax({
type: 'GET',
url: 'getSomeData',
dataType: 'text',
async: true,
success: function (data) {
alert(data);
},
error: function (data) {
alert('no data!!');
}
});

我检查了这个resource ,但我需要 javascript 部分。于是我调试并检查了开发情况。 javascript方法调用java中的action,action返回success,但是function(data)中的data并没有正确获取InputStream,它只是获取整个html网页源,如图: enter image description here

我做错了什么?提前致谢。

最佳答案

忘记 javascript 调用吧,您的问题是请求 www.yourdomain.com/getSomeData 未返回包含文本 John Doe 的页面。

为什么使用输入流?如果您使用 JSP 作为模板系统,一个简单的解决方案是

MyAction.java

private String employee;

public String someData() throws Exception{
employee= "John Doe";
return "SUCCESS";
}


public String getEmployee(){
return employee;
}

stuts.xml

<action name="getSomeData" method="someData" class="MyAction">
<result>/WEB-INF/.../templates/some_data.jsp</result>
</action>

some_data.jsp

<%= employee%>

关于java - 如何使用 Struts 将一个简单的字符串从 Action 获取到 JSP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com