How to change default JSP / template location using Struts2

I am working on a new Java EE application that uses Struts2 in Eclipse. I want to keep JSP files in the original folder ( src/main/jsp

), not in WebContent

. When deployed, all source files are copied to WebContent/WEB-INF/classes

. This also has the bonus effect that the jsp files are not directly accessible (I want everything to require action to be taken). This means that in order to display the results, I have to do this:

<result name="SUCCESS">WEB-INF/classes/index.jsp</result>

      

Is it possible to set the default location of jsp files so that it is just index.jsp

enough to reference them? Ideally, files will sit in as WEB-INF/jsp

well and not mix with classes.

I saw spring has this feature . I hope the same for Struts2.

+3


source to share


1 answer


You can create a persistent config parameter like

<constant name="struts.result.path" value="/WEB-INF/classes" />

      

Then inject this constant into your custom result dispatcher

. Add this to your default package:

<result-types>
    <result-type name="dispatcher" default="true" class="struts.results.MyServletDispatcherResult" />
</result-types>

      



The implementation is simple, you just add a prefix to the location of the result when setting it up.

public class MyServletDispatcherResult extends ServletDispatcherResult {

    private String resultPath;

    public String getResultPath() {
        return resultPath;
    }

    @Inject(value = "struts.result.path", required = false)
    public void setResultPath(String resultPath) {
        this.resultPath = resultPath;
    }

    @Override
    public void setLocation(String location) {
        super.setLocation(resultPath+location);
    }

    public MyServletDispatcherResult() {
        super();
    }

//    @Inject
//    public MyServletDispatcherResult(String location) {
//
//        super(resultPath+location);
//    }
}

      

Then you can use regular locations in the results, eg.

<result name="SUCCESS">/index.jsp</result> 

      

+2


source







All Articles