setting fullpath in controller - spring

i have developed a spring application. all requests are dispatching to controllers (i have 2 controllers in my app) so web.xml is like below
in web.xml
<servlet-mapping>
<url-pattern>/*</url-pattern>
aaa controller
#Controller
#RequestMapping("/aaa")
bbb controller
#Controller
#RequestMapping("/bbb")
but now i need to add some jsp pages into my project since the "/*" in web.xml my jsp pages are not found. so i have change the servlet-mapping like below;
in web.xml
<servlet-mapping>
<url-pattern>/aaa/*</url-pattern>
<url-pattern>/bbb/*</url-pattern>
aaa controller
#Controller
#RequestMapping("/")
bbb controller
#Controller
#RequestMapping("/")
but i do not want to use this approach since i can access xxx servlet in aaa controler like /bbb/xxx.
so is there any alternative solution, for example can i set full path in controller or anything?
thanks in advance...

You need to pass jsp through the server as well.
You can map it as html extension
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
In example-servlet.xml just add the following jsp resolver
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
and then use ModelAndView Object in your controllers:
#Controller
#RequestMapping(value="/aaa")
public class aaaController{
#RequestMapping(value="/aaa.html", method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("aaa");
return mv;
}
}
#Controller
#RequestMapping(value="/bbb")
public class aaaController{
#RequestMapping(value="/bbb.html", method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("bbb");
return mv;
}
}
In that case first controller will return /aaa.jsp as you your model andView when you hit /aaa/aaa.html
and second controller will return /bbb.jsp as you your model and View when you hit /bbb/bbb.html
Hope it helps.

Related

URL mapping is not working in web.xml in spring

Package name controller where WelcomeController is there
folder view in WEB-INF where view files are there means html and static jsp
In view welcome.jsp
In WebContent web.xml and welcome-servlet.xml are there
When I mapped / but when I changed the url-pattern then it's not working e.g. /user/* following url is working for only /
http://localhost:3000/SpringPractice/user/welcome
Error is
WARNING: No mapping found for HTTP request with URI
[/SpringPractice/user/welcome] in DispatcherServlet with name
'welcome'
it's working if I set the to /.
Even I checked the controller no error because if no mapping is found then it'd not work for / pattern.
WEB.XML
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
WelcomeController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class WelcomeController {
#RequestMapping(method=RequestMethod.GET,value="/user/welcome")
public String GET(ModelMap model){
//second is the message name
//3rd is the message
model.addAttribute("message","GET Method");
return "welcome"; //we'll always return the name of the view here welcome.jsp e.g. welcome
}
#RequestMapping(method=RequestMethod.POST,value="/user/welcome")
public String POST(ModelMap model){
model.addAttribute("message","POST Method");
return "welcome";
}
}
welcome-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Your web.xml needs to be modified if you want the servlet to map to SpringPractice as the root
Change your web.xml to look like this:
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/SpringPractice/*</url-pattern>
</servlet-mapping>
It's also possible you are using the wrong port. Tomcat (Which I am assuming you are using here) uses port 8080 by default.
The URL: http://localhost:8080/SpringPractice/user/welcome
Should now work just fine
The following is not needed, is just might be helpful
Also, you can use #RequestMapping on the class level if you wish.
#Controller
#RequstMapping(value="/user/welcome")
public class WelcomeController {
#RequestMapping(method=RequestMethod.GET, value="")
public String GET(ModelMap model){
//second is the message name
//3rd is the message
model.addAttribute("message","GET Method");
return "welcome"; //we'll always return the name of the view here welcome.jsp e.g. welcome
}
#RequestMapping(method=RequestMethod.POST, value="")
public String POST(ModelMap model){
model.addAttribute("message","POST Method");
return "welcome";
}
}
By adding RequestMapping(value="/user/welcome") to the top of your controller class all the mappings under it will use that as a base. It's nice if you know a certain controller will be handeling all requests from "www.MyCoolSite.com/user/welcome"
I hope this helps.

springmvc and url-pattern

web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
controller
#Controller
#RequestMapping("/car/*")
public class CarController extends BaseController {
#RequestMapping("baojia.html")
public ModelAndView baojia() {
ModelAndView view = new ModelAndView();
view.setViewName("baojia");
return view;
}
when i visit http://mydomain/car/baojia.html and has this error:
[carloan]2016-04-21 09:01:31,177 WARN [org.springframework.web.servlet.PageNotFound] - <No mapping found for HTTP request with URI [/views/baojia.jsp] in DispatcherServlet with name 'springMVC'>
spring.xml ViewResolver
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
and i have file in /views/boajia.jsp
whether i writer, it don't work
<mvc:resources mapping="/views/" location="/views/**" />
and i have another question, i wan't to matching this url-pattern: /api/*
and the controller is:
#Controller
#RequestMapping("/api/*")
public class CarApiController extends BaseController {
#RequestMapping("get")
#ResponseBody
public JsonResult getCars()
but it can't work
try #RequestMapping("/car") instead of #RequestMapping("/car/*")
And check below two links to understand, how request mapping defined.
can anybody explain me difference between class level controller and method level controller..?
http://duckranger.com/2012/04/advanced-requestmapping-tricks-controller-root-and-uri-templates/
URL mapping declaration is not proper use #RequestMapping("/car") and #RequestMapping("/baojia.html")

How do I map this URL to a Spring controller method?

I'm using Spring 3.1.1.RELEASE. I'm having fits mapping URLs to controller methods. I would like to map the URL "/my-context-path/organizations/add" to the controller method below. In my controller, I have
#Controller
#RequestMapping("/organizations")
public class OrganizationController
{
…
#RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView doGetadd()
{
… do some stuff …
return new ModelAndView("organizations/add");
} // doGetadd
In my web.xml I have
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>SB Admin</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/organizations/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
and in my dispathcer-servlet.xml I have
...
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="org.myco.subco" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
but requests for my desired context-path result in "No mapping found for HTTP request with URI [/myproject-1.0-SNAPSHOT/organizations/add] in DispatcherServlet with name 'dispatcher" errors (using JBoss 7). How do I map this thing properly? Note that I have multiple methods in my controller that I want to different URLs within the "/organizations" space.
Try this.
Change the dispatcher servlet mapping to :
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And for the OrganizationController the mapping would be
#Controller
#RequestMapping("/organizations")
public class OrganizationController
And for the ContractsController the mapping would be
#Controller
#RequestMapping("/contracts")
public class OrganizationController
According to the Spring Doc the ModelAndView constructor parameter is the name of the view file.
So that file could be addView.jsp .
As well as the fact that you're (as far as my Spring knowledge goes) actually mapping it to /Application-Name/organizations/organizations/add due to :
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/organizations/</url-pattern>
</servlet-mapping>
And
#Controller
#RequestMapping("/organizations")
public class OrganizationController
I'd recommend changing the requestmapping from the controller to
#Controller
#RequestMapping("/")
public class OrganizationController
The <url-pattern>/organizations/</url-pattern> basiccally defines the 'virtual path' on which your site will be accessible.
Al mappings you do on controllers will append to it, makeing it /organizations/whateverpagecomeshere.jsp
And make sure that View file exists !

Spring Controller's URL request mapping not working as expected

I have created a mapping in web.xml something like this:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/about/*</url-pattern>
</servlet-mapping>
In my controller I have something like this:
import org.springframework.stereotype.Controller;
#Controller
public class MyController{
#RequestMapping(value="/about/us", method=RequestMethod.GET)
public ModelAndView myMethod1(ModelMap model){
//some code
return new ModelAndView("aboutus1.jsp",model);
}
#RequestMapping(value="/about", method=RequestMethod.GET)
public ModelAndView myMethod2(ModelMap model){
//some code
return new ModelAndView("aboutus2.jsp",model);
}
}
And my dispatcher-servlet.xml has view resolver like:
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
To my surprise: request .../about/us is not reaching to myMethod1 in the controller. The browser shows 404 error. I put a logger inside the method but it isn't printing anything, meaning, its not being executed.
.../about works fine! What can be the done to make .../about/us request work? Any suggestions?
You need to use #RequestMapping(value="/us", method=RequestMethod.GET) or you need to request about/about/us
Since you have mapped "/about" in your web.xml, the url it will pass will be like this www.xyz.com/about/*
As your configuration says it will work for
www.xyz.com/about/about/us
www.xyz.com/about/about
In order to to work properly either use
/* in web.xml instead of /about
or change the controller's endpoint to
#RequestMapping(value="/us", method=RequestMethod.GET)
#RequestMapping(value="/", method=RequestMethod.GET)
Okay I got the thing working, here are things I added in the dispatcher-servlet.xml:
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="alwaysUseFullPath" value="true" />
</bean>

The spring web mvc framework handle the jsp file dispatch as another request

I am new to spring web mvc framework,and I use struts 2 before.
I create a new dynamic web project using eclipse EE,and add all the jars to the /web-info/lib.
The whole hierarchy of the project is like this:
SpringMVCTest
WEB-INF
web.xml
example-servlet.xml
jsp
hello.jsp
lib
xxxx.jars
.....
This is the servlet definition:
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This is the example-servlet.xml:
<context:component-scan base-package="com.kk.web.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
And the controller:
package com.kk.web.controllers;
#Controller("example")
#RequestMapping("/example")
public class ExampleController {
#RequestMapping("/hello")
#ResponseBody
public String hello() {
return "hello";
}
#RequestMapping("/hello_jsp")
public ModelAndView hello_jsp(){
ModelAndView mv=new ModelAndView("hello");
mv.addObject("message", "welcome");
return mv;
}
}
It worked when I run:
http://localhost:8080/SpringMVCTest/example/hello
But when I run:
http://localhost:8080/SpringMVCTest/example/hello_jsp
I got the warn:
2011-10-17 10:36:15 org.springframework.web.servlet.DispatcherServlet noHandlerFound
Warn: No mapping found for HTTP request with URI [/SpringMVCTest/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'example'
It seems that the ExampleController works,it dispatch the request "/example/hello_jsp" to the right view "jsp/hello.jsp".
But then the spring take the file dispatch "/jsp/hello.jsp" as another request,then it will not find the matched url mapping in the "example" controller.
Why?? IMO,a requst must come from the client to server,the controller receive only one request here "/exmaple/hello_jsp",isn't it?
And How to fix it?
BTW,I can set the url pattern to "/*.xxx",but I do not want the suffix in the url.
Any ideas?
Previous answer did not work...
This posting looks similar: http://forum.springsource.org/showthread.php?55982-No-mapping-found-for-HTTP-request-with-MVC-requests
Summary: change
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and checking my latest Spring MVC app I use the latter pattern (no * on the end).

Resources