3

I'm working on a new Java EE application that uses Struts2 in Eclipse. I want to keep the JSP files in the source folder (src/main/jsp) and not in WebContent. When deployed, the all source files get copied over to WebContent/WEB-INF/classes. This also has the bonus effect of making the jsp files inaccessible directly (I want everything to require action intervention). This means that to make results display, I have to do this:

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

Is it possible to set the default location of the jsp files so that just index.jsp is sufficient to reference them? Ideally the files would also sit in WEB-INF/jsp and not get mixed with the classes.

I saw that spring has this feature. I'm hoping for the same thing for Struts2.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Anonymous
  • 11,740
  • 3
  • 40
  • 50
  • 1
    If you are using the Convention plugin, take a look at the [Result Path configuration](http://stackoverflow.com/a/28381238/1654265). If you are not using the Convention plugin, start using the Convention plugin :) – Andrea Ligios Jul 16 '15 at 15:53
  • Trying to make it work, I'll get back to you with the results when it works/I give up. – Anonymous Jul 16 '15 at 16:12
  • Looks like it requires usage of annotations which is a no go, unfortunately. I guess I'll just stick to repetitive paths. – Anonymous Jul 16 '15 at 17:29
  • Are you using maven? Put your jsp-s into `webapp/WEB-INF/jsp/` in that way there are not mixed with classes. As for the path take a look at tiles. – Aleksandr M Jul 16 '15 at 17:47
  • I'd love to use maven, but workplace enforces ant only. But in any case, I want the jsp files in the `src` folder. – Anonymous Jul 16 '15 at 17:49
  • In maven `webapp` folder is in the `src` -> `src/main/webapp`. – Aleksandr M Jul 16 '15 at 17:50
  • Why do you put jsp-s in src if you don't use maven? You can copy them into desired place when you are building war. – Aleksandr M Jul 16 '15 at 17:52
  • Because jsp files are also source files. But that's unrelated as even putting them directly into `WEB-INF/pages` still requires me to put the full path which is the original issue. – Anonymous Jul 16 '15 at 18:02
  • 1
    I don't think there's a config option for the root of JSP files, regardless where they live in the pre-packaged project. You could modify the default result types to allow this, which should be pretty easy. – Dave Newton Jul 16 '15 at 19:51

1 Answers1

2

You can create a constant configuration parameter, e.g.

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

Then inject this constant into custom dispatcher result. Add this to your default package:

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

The implementation is easy, you just add a prefix to the location of the result when it's configured.

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 ordinary locations in the results, e.g.

<result name="SUCCESS">/index.jsp</result> 
Roman C
  • 49,761
  • 33
  • 66
  • 176