I'm having an issue regarding #RequestMapping on classes. Say I have these two controllers:
#Controller
#RequestMapping(value="/controller1")
public class Controller1 {
#RequestMapping(value="/method11.do")
public #ResponseBody method11(){
//...
}
#RequestMapping(value="/method12.do")
public ModelAndView method12(){
//This method redirects me to another jsp where I'll call Controller2 methods
return new ModelAndView("test");
}
}
#Controller
#RequestMapping(value="/controller2")
public class Controller2 {
#RequestMapping(value="/method21.do")
public #ResponseBody method21(){
//...
}
}
When I first call via AJAX method11, it works fine, the url generated is http://mydomain/myapp/controller1/method11.do
Then, I call method12 and get redirected to test.jsp, and from there, I call to method21, and here is the problem, the url generated is not the expected http://mydomain/myapp/controller2/method21.do, but something else, depending on how I make the AJAX call:
url:'controller2/method21' --> http://mydomain/myapp/controller1/controller2/method21.do
url:'/controller2/method21' --> http://mydomain/controller2/method21.do
So, in what way should I make the calls so that they always start at http://mydomain/myapp/...?
I believe I could just use url:'/myapp/controller2/method21.do', but I guess there should be a more generic way in which I don't have to use 'myapp' on every call.
This is my web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
You should make the client aware of the proper URL by retrieving the context root within your script using JSP EL.
In JSP
<script>var ctx = "${pageContext.request.contextPath}"</script>
You can then use ctx as a prefix to the URLs constructed via Javascript.
var url = ctx + "/rest_of_url"
On the server side, you can use:
${pageContext.request.contextPath} or JSTL has a tag, <c:url> which will append your context root.
Related
In our app for all the notification we trigger through mail.
All the templates have non sso link
>/Userlogin?param1=param2value¶m2=param2value">Link to access app
I need to modify this link in all templates to
>/Userloginsso?param1=param2value¶m2=param2value">Link to access app
Since there are many templates and takes lot of manual effort, is there any way we can redirect the request of Userlogin to Userloginsso. Any configuration that we can do in web.xml ?
Considering you have a mapping for Userlogin in web.xml as below:
<web-app>
<servlet>
<servlet-name>Userlogin</servlet-name>
<servlet-path>com.something.Userlogin</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Userlogin</servlet-name>
<url-pattern>/Userlogin</url-pattern>
</servlet-mapping>
</web-app>
Modify existing mapping to :
<web-app>
<servlet>
<servlet-name>Userloginsso</servlet-name>
<servlet-path>com.something.Userloginsso</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Userloginsso</servlet-name>
<url-pattern>/Userlogin</url-pattern>
</servlet-mapping>
</web-app>
Now all calls to Userlogin will be redirected to Userloginsso servlet.
You could do a simple redirect in your UserLogin servlet with the following:
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException {
String param1 = request.getParameter ("param1");
String param2 = request.getParameter ("param2");
// other parameters
// Build the new url: if too much parameters, prefer using a StringBuilder over String concatenation for better performances
String baseUrl = request.getContextPath () + "/Userloginsso?param1=" + param1 + "¶m2=" + param2;
String encodedUrl = response.encodeRedirectURL (baseUrl);
response.sendRedirect (encodedUrl);
}
If I understand your question correctly you could use a filter example here get the url and foward it somplace else in your app. Or and url rewrite library such us this one
If you still want a servlet you could use a ProxyServlet. There are already many good implementations.
Examples:
Complex proxy servlet with all features
Simple proxy servlet, limited features
I have a Controller which is designed to accept all requests and do all the required processing. However it is not working for all requests. My Controller is such:
#RequestMapping("/*")
public class GatewayDummyController
and my web.xml configuration is:
<servlet-name>sample-apis-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<servlet-mapping>
<servlet-name>sample-apis-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The name of the war is sampleApi. I am able to access urls:
localhost/8080/sampleApi
localhost/8080/sampleApi/rest
the URL that I cannot access is:
localhost/8080/sampleApi/rest/v1.
I need to access all urls after sampleApi/. Please suggest a way to do that.
Add #RequestMapping("/**") to the controller to accept all urls
I am writing spring mvc application.
In my application I have web pages as well as rest web services to handle ajax call.
I have done below entry in web.xml
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_myapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Should I map my rest url with same servlet like
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Or should I make new servlet entry for rest.
I have done required entries in pom.xml for "org.codehaus.jackson" and also I have made required entries in my spring_myapp-servlet.xml.
For html page I am using below code in my controller
#RequestMapping(value = "/htmlUrl")
public ModelAndView ModifyValiodation(HttpServletRequest request) {
// my code
}
For rest service I am using
#RequestMapping(value = "/restUrl")
public #ResponseBody Map<String, String> restUrl(HttpServletRequest request) {
// my code
}
If I am using only one servlet for two url mapping, then total 4 url will be made.
myapp/htmlUrl.html
myapp/restUrl.html
myapp/rest/htmlUrl
myapp/rest/restUrl
If I am using two different servlet with individual dispacherServlet then will i have to make entry of every component and service of spring in both the servlet.xml?
Please point out the solution for exposing rest web service.
Thanks!
use
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
If you use two DispatcherServlet entries , it will load two ApplicationContext Objects in your application. Since you are using spring mvc to handle all the requests to your app, you should be fine with this configuration. Any request url that ends with .html or any urls that contains /rest/ will be handled by spring.
It is up to you to design the server side of the infrastructure.
Neither the RESTful specifications have any instructions for doing this nor the Servlet specifications enforce anything on this.
On the Applications design I think it is better idea to keep two different servlets to handle different URLs because over time the classes will become complex and long. These to may be used as front controllers and may have common logic class in the backend.
I have spring mvc application
if in web.xml i write so:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I go to http://localhost:8080/Mvc/controllerPath/sayHello
I see my page
if I write
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/controllerPath/*</url-pattern>
</servlet-mapping>
I go to http://localhost:8080/Mvc/controllerPath/sayHello - I see 404
I think you understood what I want.
Can you hel me?
UPDATE
controller:
#Controller
#RequestMapping("/controllerPath")
public class MyController {
#RequestMapping("/sayHello")
public String sayHello(Model model){
model.addAttribute("name", "Vasya");
return "hello";
}
}
if I write
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/Mvc/controllerPath*</url-pattern>
</servlet-mapping>
i see 404
If you remove:
#RequestMapping("/controllerPath")
to
#RequestMapping("/")
the new servlet mapping will work.
The reason for this is that whatever you have in the servlet mapping url is stripped before spring tries to match it to a controller.
For example, in your first url mapping with just / (and assuming your web appllication is deployed to /mvc), your path of /mvc/controllerPath/sayHello spring strips the url mapping away from the url and expects to find a controller that maps to /controllerPath/sayHello
When you change the url-pattern to /controllerPath, since will strip that as well and look for a controller that answers to just /sayHello, which your controller won't since it's expecting /controllerPath/sayHello
http://127.0.0.1:8888/socialnetwork/contactsService
That's the current URL for one of my servlets. My question is, how do I change it?
In my web.xml file, altering
<servlet-mapping>
<servlet-name>contactsServiceServlet</servlet-name>
<url-pattern>/socialnetwork/contactsService</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>contactsServiceServlet</servlet-name>
<url-pattern>/a/contactsService</url-pattern>
</servlet-mapping>
Makes absolutely NO difference to the URL it requests when I make an RPC-call to the servlet.
Once you have done the above you need to change where you invoke (Which is described in the Annotation below) as in...
// The RemoteServiceRelativePath annotation automatically calls setServiceEntryPoint()
#RemoteServiceRelativePath("email")
public interface MyEmailService extends RemoteService {
void emptyMyInbox(String username, String password);
}
See http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/rpc/RemoteServiceRelativePath.html