I need to write an ant task to determine if a certain file is readonly, and if it is, fail. I would like to avoid using a custom selector to do this do to the nature of our build system. Anyone have any ideas how to go about doing this? I'm using ant 1.8 + ant-contrib.
Thanks!
Will something like this do the trick?
<condition property="file.is.readonly">
<not>
<isfileselected file="${the.file.in.question}">
<writable />
</isfileselected>
</not>
</condition>
<fail if="file.is.readonly" message="${the.file.in.question} is not writeable" />
This uses the condition task and the isfileselected condition (not a direct link - you'll have to search down the page) combined with the writable selector (and reversed with a not condition).
Update:
An possibly better alternative would be:
<fail message="${the.file.in.question} is not writeable">
<condition>
<not>
<isfileselected file="${the.file.in.question}">
<writable />
</isfileselected>
</not>
</condition>
</fail>
This has the check and the fail as one distinct action rather than two, so you may find it clearer, and it doesn't require using a property name, so your namespace is cleaner.
I'm sure there are better ways but I'll throw a few possible methods out there.
Use the copy task to create a temp duplicate then attempt to copy this file to overwrite the original. The failonerror attribute will come in handy
Use the java task to execute a task which executes some simple code such as:
File f = new File( path );
f.canWrite()......
What about writing a custom condition to be used by the condition task? It is more flexible.
public class IsReadOnly extends ProjectComponent implements Condition
{
private Resource resource;
/**
* The resource to test.
*/
public void add(Resource r) {
if (resource != null) {
throw new BuildException("only one resource can be tested");
}
resource = r;
}
/**
* Argument validation.
*/
protected void validate() throws BuildException {
if (resource == null) {
throw new BuildException("resource is required");
}
}
public boolean eval() {
validate();
if (resource instanceof FileProvider) {
return !((FileProvider)resource).getFile().canWrite();
}
try {
resource.getOutputStream();
return false;
} catch (FileNotFoundException no) {
return false;
} catch (IOException no) {
return true;
}
}
}
Integrate with
<typedef
name="isreadonly"
classname="IsReadOnly"
classpath="${myclasses}"/>
and use it like
<condition property="readonly">
<isreadonly>
<file file="${file}"/>
</isreadonly>
</condition>
Related
I'm trying to implement an IVirtualImageProvider plugin for ImageResizer as explained here. I didn't find the instructions hard to follow, however it doesn't seem like any of my images are passing through the plugin. My images are stored in a Windows folder located outside of the ASP .NET root.
Any image path that starts with "assets" or "images" should be handled by the plugin. Here is my implementation of the IVirtualImageProvider and IVirtualFile interfaces:
namespace ImageResizer.Plugins.Basic
{
public class ResizerVirtFolder : IPlugin, IVirtualImageProvider
{
public IPlugin Install(Configuration.Config c)
{
c.Plugins.add_plugin(this);
return this;
}
public bool Uninstall(Configuration.Config c)
{
c.Plugins.remove_plugin(this);
return true;
}
public bool FileExists(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
return (virtualPath.StartsWith("assets", StringComparison.OrdinalIgnoreCase) || virtualPath.StartsWith("images", StringComparison.OrdinalIgnoreCase));
}
public IVirtualFile GetFile(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
return new ResizerVirtualFile(virtualPath);
}
}
public class ResizerVirtualFile : IVirtualFile
{
public ResizerVirtualFile(string virtualPath)
{
this._virtualPath = virtualPath;
}
protected string _virtualPath;
public string VirtualPath
{
get { return _virtualPath; }
}
public System.IO.Stream Open()
{
string sitePath = System.Configuration.ConfigurationManager.AppSettings["PageFilesLocation"];
_virtualPath = _virtualPath.Contains("assets/") ? _virtualPath.Substring(_virtualPath.IndexOf("assets/") + 7) : _virtualPath;
string assetPath = Path.Combine(sitePath, _virtualPath.TrimStart('/').Replace("/", #"\"));
System.IO.FileStream oStream = new FileStream(assetPath, FileMode.Open);
return oStream;
}
}
}
Here's a brief snippet of the Web.config modification I made for the plugin:
<resizer>
<plugins>
<add name="MvcRoutingShim" />
<add name="ResizerVirtFolder" />
</plugins>
</resizer>
ImageResizer.Plugins.Basic.ResizerVirtFolder shows up under registered plugins when I go to resizer.debug.ashx, so I believe that means the plugin is loaded. However, when I put a breakpoint on the FileExists or GetFile functions, it isn't triggered.
I thought to use the VirtualFolder plugin, but it doesn't look like it's included in the download any more. I'm using v 3.4.3.
Edit: Added link to the debug output Gist here.
Longer Edit: I should add that the images that are not showing up do not have query strings in their requests and are not being resized in any way. Does that mean that ImageResizer will not look at them at all, and as a result, the Virtual Image Provider's functions will not be executing in this case?
Another Edit: Looking at this page, it seems like the simplest way to get the images to work in ImageResizer might be to add a different prefix rather than /assets or /images, like perhaps /resize. In this case, should I add an ignore route for /resize or not? There is a route handler provided by my CMS which will eventually try to deal with this route if I do not ignore it.
Well, looks like I found my own solution. Here's the problem:
return (virtualPath.StartsWith("assets", StringComparison.OrdinalIgnoreCase)
StartsWith will always return false because the virtualPath will start with my hostname. Switching to a Contains() statement has this working perfectly.
Great plugin, by the way!
How to Integrate Struts Conventions with Tiles while maintaining conventions benefits?
The issue is that conventions links url-to-action-to-result automatically and does this nicely for jsp, velocity and freemarker results. It does not expect to deal with a tiles result.
When using tiles we typically want all our UI actions (as opposed the json/xml service actions) to use tiles but in doing so we lose the convention for the result component and need to use annotations. Annotations allow us to deviate from the expected, but in a large application when expecting to use tiles this is an annoyance. Further conventions allows us to create actions by only specifying a view. We would want to retain such benefit when using tiles as well. To rectify this we need to establish a convention that carries though to the tiles result such that we don't need to use annotations to tie the action to the tiles result and that we can continue to create JSPs without actions classes which will gain the benefits of conventions (no xml) and the benefits of tiles (all the boiler plate is factored into tiles).
How to achieve this?
This is a self answer to help others who wish to address this issue
Here are the steps needed:
Create custom tiles result which dynamically builds a "location" string (the location string is the value passed to tiles) which takes into account the namespace, actionName.
Create a package which uses this result (named "tiles") and have conventions use that as it's parent package
Implement and register a "com.opensymphony.xwork2.UnknownHandler", this step is the most critical as this handler is called when the result can't be resolved
Tiles definition(s) which make use of "location" passed in from the first step
The above steps require the following in struts.xml
<struts>
<constant name="struts.convention.default.parent.package" value="tiles-package"/>
<bean type="com.opensymphony.xwork2.UnknownHandler" name="tilesUnknownHandler" class="com.kenmcwilliams.tiles.result.TilesUnknownHandler"/>
<package name="tiles-package" extends="convention-default">
<result-types>
<result-type default="true" name="tiles" class="com.kenmcwilliams.tiles.result.TilesResult"/>
</result-types>
</package>
</struts>
Custom result-type implementation:
package com.kenmcwilliams.tiles.result;
import com.opensymphony.xwork2.ActionInvocation;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
public class TilesResult extends ServletDispatcherResult {
private static final Logger log = Logger.getLogger(TilesResult.class.getName());
public TilesResult() {
super();
}
public TilesResult(String location) {
super(location);
}
#Override
public void doExecute(String location, ActionInvocation invocation) throws Exception {
//location = "test.definition"; //for test
log.log(Level.INFO, "TilesResult doExecute() location: {0}", location);
//Start simple conventions
//
if (/** tiles && **/location == null) {
String namespace = invocation.getProxy().getNamespace();
String actionName = invocation.getProxy().getActionName();
location = namespace + "#" + actionName + ".jsp"; //Warning forcing extension
log.log(Level.INFO, "TilesResult namespace: {0}", namespace);
log.log(Level.INFO, "TilesResult actionName: {0}", actionName);
log.log(Level.INFO, "TilesResult location: {0}", location);
}
//End simple conventions
setLocation(location);
ServletContext context = ServletActionContext.getServletContext();
ApplicationContext applicationContext = ServletUtil.getApplicationContext(context);
TilesContainer container = TilesAccess.getContainer(applicationContext);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletRequest servletRequest = new ServletRequest(applicationContext, request, response);
container.render(location, servletRequest);
}
}
TilesUnknownHandler Implementation:
package com.kenmcwilliams.tiles.result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.XWorkException;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.config.entities.ResultConfig.Builder;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import flexjson.JSONSerializer;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.ConventionUnknownHandler;
public class TilesUnknownHandler extends ConventionUnknownHandler {
private static final Logger log = Logger.getLogger(TilesUnknownHandler.class.getName());
private static final String conventionBase = "/WEB-INF/content";
#Inject
public TilesUnknownHandler(Configuration configuration, ObjectFactory objectFactory,
ServletContext servletContext, Container container,
#Inject("struts.convention.default.parent.package") String defaultParentPackageName,
#Inject("struts.convention.redirect.to.slash") String redirectToSlash,
#Inject("struts.convention.action.name.separator") String nameSeparator) {
super(configuration, objectFactory, servletContext, container, defaultParentPackageName,
redirectToSlash, nameSeparator);
log.info("Constructed TilesUnknownHandler");
}
#Override
public ActionConfig handleUnknownAction(String namespace, String actionName)
throws XWorkException {
ActionConfig actionConfig;
log.info("TilesUnknownHandler: before handleUnknownAction");
ActionConfig handleUnknownAction = super.handleUnknownAction(namespace, actionName);
log.info("TilesUnknownHandler: after handleUnknownAction, returning with:");
log.log(Level.INFO, "...ActionConfig value: {0}", (new JSONSerializer().serialize(handleUnknownAction)));
log.log(Level.INFO, "Modifying handleUnknowAction result handler");
Map<String, ResultConfig> results = handleUnknownAction.getResults();
ResultConfig resultConfig = results.get("success");
Builder builder = new ResultConfig.Builder("com.opensymphony.xwork2.config.entities.ResultConfig", "com.kenmcwilliams.tiles.result.TilesResult");
Map<String, String> params = resultConfig.getParams();
String tilesResultString = null;
String location = params.get("location");
if (location != null && !location.isEmpty()) {
int length = conventionBase.length();
if(StringUtils.startsWith(location, conventionBase)){
String subString = location.substring(length); //chop off "/WEB-INF/content"
int count = StringUtils.countMatches(subString, "/");//TODO: maybe check for "//", although I don't know why it would be in the string
if (count == 1){//empty namespace
tilesResultString = subString.replaceFirst("/", "#"); //TODO: because I am doing a straight replacement of the last element the else can probably be removed
}else{ //replace the last slash between the namespace and the file with "#"
int lastIndex = subString.lastIndexOf("/");
//subString.substring(lastIndex, lastIndex);
String nameSpace = subString.substring(0, lastIndex);
String file = subString.substring(lastIndex + 1);
tilesResultString = nameSpace + "#" + file;
}
}
}
Map<String, String> myParams = new LinkedHashMap<String, String>();
myParams.put("location", tilesResultString);
builder.addParams(myParams);
ResultConfig build = builder.build();
Map<String, ResultConfig> myMap = new LinkedHashMap<String, ResultConfig>();
myMap.put("success", build);
log.log(Level.INFO, "\n\n...results: {0}\n\n", (new JSONSerializer().serialize(results)));
actionConfig = new ActionConfig.Builder(handleUnknownAction).addResultConfigs(myMap).build();
//className("com.kenmcwilliams.tiles.result.TilesResult")
return actionConfig;
}
#Override
public Result handleUnknownResult(ActionContext actionContext, String actionName,
ActionConfig actionConfig, String resultCode) throws XWorkException {
log.info("TilesUnknownHandler: before handleUnknownResult");
Result handleUnknownResult = super.handleUnknownResult(actionContext, actionName, actionConfig, resultCode);
log.info("TilesUnknownHandler: after handleUnknownResult, returning with:");
log.log(Level.INFO, "...Result value: {0}", (new JSONSerializer().serialize(handleUnknownResult)));
return handleUnknownResult;
}
}
An example of how to use our "location" string which is in the form of: NameSpace + "#" + ActionName + ".jsp", note this definition <definition name="REGEXP:(.*)#(.*)" extends="default"> in the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="default" template="/WEB-INF/template/template.jsp">
<put-list-attribute name="cssList" cascade="true">
<add-attribute value="/style/cssreset-min.css" />
<add-attribute value="/style/cssfonts-min.css" />
<add-attribute value="/style/cssbase-min.css" />
<add-attribute value="/style/grids-min.css" />
<add-attribute value="/script/jquery-ui-1.8.24.custom/css/ui-lightness/jquery-ui-1.8.24.custom.css" />
<add-attribute value="/style/style.css" />
</put-list-attribute>
<put-list-attribute name="jsList" cascade="true">
<add-attribute value="/script/jquery/1.8.1/jquery.min.js" />
<add-attribute value="/script/jquery-ui-1.8.24.custom/js/jquery-ui-1.8.24.custom.min.js" />
<add-attribute value="/script/jquery.sort.js" />
<add-attribute value="/script/custom/jquery-serialize.js" />
</put-list-attribute>
<put-attribute name="title" value="defaults-name" cascade="true" type="string"/>
<put-attribute name="head" value="/WEB-INF/template/head.jsp"/>
<put-attribute name="header" value="/WEB-INF/template/header.jsp"/>
<put-attribute name="body" value="/WEB-INF/template/body.jsp"/>
<put-attribute name="footer" value="/WEB-INF/template/footer.jsp"/>
</definition>
<definition name="REGEXP:(.*)#(.*)" extends="default">
<put-attribute name="title" cascade="true" expression="OGNL:#com.opensymphony.xwork2.ActionContext#getContext().name"/>
<put-attribute name="body" value="/WEB-INF/content{1}/{2}"/>
</definition>
</tiles-definitions>
With this in place you can create JSP's under /WEB-INF/content/someplace/my-action.jsp
Just as you would with conventions AND tiles will decorate it appropriately as well if you create an action class called com.myapp.action.someplace.MyAction without any result type this code will execute and the /WEB-INF/content/someplace/my-action.jsp result would still be rendered.
There you have it conventions + tiles with no more annotations (well for the normal case).
NOTES:
This answer certainly isn't perfect but it does provide a working example of the strategy which can be applied to other view technologies (sitemesh, others).
Currently you can see the ".jsp" is being appended in the tiles result NOT in the tiles definitions this is inflexible. The specific extension should be specified within tiles, that is the body attribute within the definition should append the specific view type (.jsp, .fml, .vm) because you should know best at that time.
It is important to note that definitions are tried in the order they are given,so you can override the normal case REGEXP:(.*)#(.*) by placing definitions between the default and REGEXP:(.*)#(.*) definitions. For instance a definition called authenticated\(.*) can be placed between these two definitions. After all if you couldn't do this and all pages had to be tiled the same we really wouldn't be using tiles!
Just so you know when using tiles3 (the struts2 tiles3 plugin) you can use all three types of view technologies (jsp, freemarker, velocity) to compose one tile. It works. You are probably going to use one view technology consistently but it's nice to know it is possible.
I have the following construct at several places in my webapp in order to conditionally render page fragments depending on some actions:
<h:panelGroup rendered="#{managedBean.serviceSelected == 'insurance'}">
<ui:include src="/pages/edocket/include/service1.xhtml" />
</h:panelGroup>
I have observed, that the <ui:include> is still executed even when the rendered attribute evaluates false. This unnecessarily creates all backing beans associated with the service1.xhtml file which is been included.
How can I skip executing the <ui:include> when the parent UI component is not rendered, so that all those backing beans are not unnecessarily created?
Unfortunately, this is by design. The <ui:include> runs as being a taghandler during the view build time, while the rendered attribute is evaluated during the view render time. This can be better understood by carefully reading this answer and substituting "JSTL" with "<ui:include>": JSTL in JSF2 Facelets... makes sense?
There are several ways to solve this, depending on the concrete functional requirement:
Use a view build time tag like <c:if> instead of <h:panelGroup>. This however puts implications into the #{managedBean}. It can't be view scoped and should do its job based on HTTP request parameters. Exactly those HTTP request parameters should also be retained in subsequent request (by e.g. <f:param>, includeViewParams, etc) so that it doesn't break when the view is restored.
Replace <ui:include> by a custom UIComponent which invokes FaceletContext#includeFacelet() during the UIComponent#encodechildren() method. So far no such component exist in any of the existing libraries. But I can tell that I've already such one in mind as a future addition for OmniFaces and it works as intuitively expected here at my test environment (with Mojarra). Here's a kickoff example:
#FacesComponent(Include.COMPONENT_TYPE)
public class Include extends UIComponentBase {
public static final String COMPONENT_TYPE = "com.example.Include";
public static final String COMPONENT_FAMILY = "com.example.Output";
private enum PropertyKeys {
src;
}
#Override
public String getFamily() {
return COMPONENT_FAMILY;
}
#Override
public boolean getRendersChildren() {
return true;
}
#Override
public void encodeChildren(FacesContext context) throws IOException {
getChildren().clear();
FaceletContext faceletContext = ((FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY));
faceletContext.includeFacelet(this, getSrc());
super.encodeChildren(context);
}
public String getSrc() {
return (String) getStateHelper().eval(PropertyKeys.src);
}
public void setSrc(String src) {
getStateHelper().put(PropertyKeys.src, src);
}
}
Use conditional expression as ui:include src:
<h:panelGroup>
<ui:include
src="#{managedBean.serviceSelected == 'insurance' ?
'/pages/edocket/include/service1.xhtml'
:
'/pages/empty.xhtml'}"
/>
</h:panelGroup>
I am new to Spring Security. I have been working on creating a custom voter that will decide whether to grant permission or not based on the value of an attribute of the object. That is, if object instance A has attribute X with value i, user with ROLE_MGR has access. If object instance B has value j in the X attribute, then ROLE_MGR does not have access. is it possible to do that and if so, what do I need to do? if this is not possible we may decide not to use Spring Security.
Thats possible, but first have a look at Spring Securitys domain object security. This is used to grant fine grained access on your objects, see here: http://static.springsource.org/spring-security/site/docs/3.0.x/reference/domain-acls.html
I figured it out. I need to use a custom permission evaluator. The snippets from my code are provided below for anyone that might be trying to do something similar:
security.xml
<security:global-method-security
pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler" />
</security:global-method-security>
<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator">
<bean id="permissionEvaluator"
class="org.krams.tutorial.infrastructure.SomePermissionsEvaluator" />
</property>
</bean>
Service Interface
#PostFilter("hasPermission(filterObject, 'READ')")
public List getAll();
Custom Permissions Evaluator
#Override
public boolean hasPermission(Authentication authorities,
Object targetDomainObject, Object permission) {
boolean Decision = false;
System.out.println("Initial Decision: " + Decision);
Date cutoffDate = null;
try {
cutoffDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH)
.parse("January 1, 2012");
System.out.println("Cutoff Date: " + cutoffDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Domain Object Date: "
+ Post.class.cast(targetDomainObject).getDate());
if (Post.class.cast(targetDomainObject).getDate().before(cutoffDate)) {
Decision = false;
System.out.println("In before");
} else {
Decision = true;
System.out.println("In after");
}
System.out.println("Final Decision: " + Decision);
System.out.println("--------");
return Decision;
}
I have a struts2 application which uses the struts2-rest-plugin v.2.2.3.
Everything is working great when it comes to the routing of the requests to the actions and its methods and I'm also using ModelDriven to specify the object to serialise when using extensions like JSON and XML.
The problem I'm having is that when I send a POST or PUT request to the struts layer I just get an empty response.
I am sending a POST request to the action like so: http://localhost:8080/alert-settings!update.json. I have a breakpoint in that method and it gets called and the code runs and completes. I have a feeling the issue might be that I am trying to use the ModelDriven interface to send me back the response and for some reason the rest-plugin doesn't like this but I don't know why it would behave like that.
Is there a known issue with receiving responses from POST requests while using the rest plugin? I have looked everywhere and can't find anything about it really.
Any help appreciated and I can provide any more details on request.
I encountered the same issue. Have you tried to set in the struts.xml file:
struts.rest.content.restrictToGET = false
See the last setting on the rest plugin docs
I actually figured out that it was a line in the rest plugin causing this:
// don't return any content for PUT, DELETE, and POST where there are no errors
if (!hasErrors && !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
target = null;
}
This is in org.apache.struts2.rest.RestActionInvocation in the selectTarget() method. I find this to be quite annoying as it doesn't really follow the REST architecture, id like the option to be able to return response objects for POST, DELETE and PUT requests in some cases.
I worked around this by extending RestActionProxyFactory and RestActionInvocation and specifying the use of this in my struts xml like so:
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="restOverride" class="uk.co.ratedpeople.tp.rest.RPRestActionProxyFactory" />
<constant name="struts.actionProxyFactory" value="restOverride" />
This allows me to use the struts plugin throughout while returning object on POST requests.
RestActionProxyFactory
public class RPRestActionProxyFactory extends RestActionProxyFactory {
#Override
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map extraContext, boolean executeResult, boolean cleanupContext) {
if (namespace.startsWith(this.namespace)) {
ActionInvocation inv = new RPRestActionInvocation(extraContext, true);
container.inject(inv);
return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
} else {
return super.createActionProxy(namespace, actionName, methodName, extraContext, executeResult, cleanupContext);
}
}
}
RestActionInvocation
public class RPRestActionInvocation extends RestActionInvocation {
public RPRestActionInvocation(Map extraContext, boolean pushAction) {
super(extraContext, pushAction);
}
#SuppressWarnings("unchecked")
#Override
protected void selectTarget() {
// Select target (content to return)
Throwable e = (Throwable)stack.findValue("exception");
if (e != null) {
// Exception
target = e;
hasErrors = true;
} else if (action instanceof ValidationAware && ((ValidationAware)action).hasErrors()) {
// Error messages
ValidationAware validationAwareAction = ((ValidationAware)action);
Map errors = new HashMap();
if (validationAwareAction.getActionErrors().size() > 0) {
errors.put("actionErrors", validationAwareAction.getActionErrors());
}
if (validationAwareAction.getFieldErrors().size() > 0) {
errors.put("fieldErrors", validationAwareAction.getFieldErrors());
}
target = errors;
hasErrors = true;
} else if (action instanceof ModelDriven) {
// Model
target = ((ModelDriven)action).getModel();
} else {
target = action;
}
// don't return any content for PUT, DELETE, and POST where there are no errors
// if (!hasErrors && !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
// target = null;
// }
}
}
I've used struts actions with mixed result types in the past, returning json, xml, and tiles for instance. I'm not sure if it's the recommended way to do it but it requires some configuration using struts.xml even though conventions are being used. Maybe you've already done this, not sure there isn't enough info provided to tell.
Struts.xml settings:
<constant name="struts.convention.default.parent.package" value="restful"/>
<package name="restful" extends="rest-default, struts-default, json-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
<result-type name="json" class="com.googlecode.jsonplugin.JSONResult"/>
</result-types>
....
</package>
I have setup the extra result types to be used on specific actions later. In the action class you can then setup your result types by action or method.
Action Class:
#Results({
#Result(name = "JsonSuccess", type = "json"),
#Result(name = "success", type = "tiles", location = "/tickets.tiles")
})
public class EventController extends RestActionSupport implements ModelDriven<EventBean>{
...
}
Something else to note about json results, I've noticed that when I have a serializable object being returned as a result, if that object contains other complex objects with a getter/setter that returns the embedded object, I will often receive an empty result or no result. I often end up writing json wrapper objects to use for my json results with getters/setters that only return java types (String, int, Boolean, etc) and not the embedded objects. I think that've solved this using delegate getters/setters but I'll have to go back and look at some old code.