我的解决方案:
两个 WebScript,一个调用作为重定向到第二个。
文件:RedirectHelloWorldWebScript.java:
public class RedirectHelloWorldWebScript extends AbstractWebScript {
@Override
public void execute(WebScriptRequest wsreq, WebScriptResponse wsres)
throws IOException {
HttpServletResponse httpResponse = WebScriptServletRuntime
.getHttpServletResponse(wsres);
httpResponse.sendRedirect("/alfresco/service/helloworld");
}
}
文件:HelloWorldWebScript.java:
public class HelloWorldWebScript extends AbstractWebScript {
@Override
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException {
try {
JSONObject obj = new JSONObject();
obj.put("message", "Hello Word!");
String jsonString = obj.toString();
res.getWriter().write(jsonString);
}
catch (JSONException e) {
throw new WebScriptException("Unable to serialize JSON");
}
catch (org.json.JSONException e) {
e.printStackTrace();
}
}
}
描述符:
文件:redirecthelloworld.get.desc.xml:
<webscript>
<shortname>RedirectHelloWorld</shortname>
<description>Redirect to Hello World</description>
<url>/redirecthelloworld</url>
<authentication>none</authentication>
<family>Java-Backed WebScripts</family>
</webscript>
文件:helloworld.get.desc.xml:
<webscript>
<shortname>helloworld</shortname>
<description>Hello World</description>
<url>/helloworld</url>
<url>/helloworld.json</url>
<authentication>none</authentication>
<family>Java-Backed WebScripts</family>
</webscript>
还有,Spring 的上下文:
文件:webscript-context.xml:
<bean id="webscript.helloworld.get"
class="com.fegor.HelloWorldWebScript"
parent="webscript">
</bean>
<bean id="webscript.redirecthelloworld.get"
class="com.fegor.RedirectHelloWorldWebScript"
parent="webscript">
</bean>
祝你好运!