Struts 2 - How to use package in URL

168 Views Asked by At

I would like to build a dynamic web project like that:

Method: GET | POST | DELETE API URL: http://example.com/api/

Example:

So can you help me How can I do it by using STRUTS 2. Please explain something about it if you can.

2

There are 2 best solutions below

2
Kartik73 On

You can do this by using namespace attribute of the Package tag in your struts configuration file struts.xml.

Below is the example for doing this.

<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
    <result>pages/login.jsp</result>
</action>
</package>
0
Dave Newton On

Use the S2 REST plugin.

It sets up everything you need, including result type mapping based on request URL. You might need to do some tweaking for the multi-level bits. If it doesn't work out-of-the-box, you can use the following along with various URL mapping options (including the package namespacing mentioned by Kartik).

Alternatively, you could do it manually using a combination of parameter name matching and the JSON plugin (or use whatever result types you need).