Spring MVC request Mapping Class level - java

I have made a small spring mvc app. In my controller i have 2 methods which returns the names of the jsp files in the web-inf folder.
Now the application works perfectly, but if i try to add a url path it doesn't work.
What i mean is something like this:
#Controller
#RequestMapping("/start") //if add this it doesn't work
public class SalesController {
#RequestMapping(value = "/greeting")
public String sayGreeting(Model model) {
model.addAttribute("greetingMsg", "Hello Spring");
return "welcome";
}
#RequestMapping(value = "/hello")
public String getHello(Model model) {
model.addAttribute("greeting", "Yo man");
return "hello";
}
}
Here is my servletConfig configuration
<mvc:annotation-driven />
<context:component-scan base-package="com.myCompany" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
If i give the path "myApplicationName"/start/greeting it give error. But if i remove start it works.
What seems to be the problem here?
Thank you
Update:
Below is my web.xml configuration
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

Add /.html* in your URL Pattern:
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>/*.html</url-pattern> </servlet-mapping>

Related

Spring MVC app not running, throwing 404 when accessing

I have started some spring mvc application and deployed to WAR to web server. Deployed with out any issue but when using the URL on browser, i'm getting 404 error.
Web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
</web-app>
App Struture
Controller class
public class WeatherController {
#RequestMapping(value = "/CityWeather", method = RequestMethod.GET)
public String weather( Model model) {
return "home";
}
}
servlet-context.xml
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.weather" />

Where I can find my WADL? (CXF)

I have developed a simple RESTful service using Apache CXF coupled in a web application and is working fine.
I Can access it on "http://localhost:8080/SpringRestProjectJava/api/books/1234" and I am also getting the proper JSON response.
My understanding is that from this link that WADL will be autogenerated.
Is it correct? If yes, how can I see WADL for this service.
This is my 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>/WEB-INF/CustomSpringConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simplerest</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>org.gsdev.ws.bookservice.BookResource</param-value>
</init-param>
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>org.gsdev.ws.bookservice.provider.XstreamJsonProvider</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simplerest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
This is my BookResource.java
#Path( "books/{isbn}" )
public class BookResource {
#GET
#Produces ( "application/json" )
public Book getDetails( #PathParam("isbn") String isbn){
if( isbn.equals( "1234" )){
Book book = new Book();
book.setIsbn(isbn);
book.setTitle("Learning web services by Garry");
return book;
}
return null;
}
}
Finally, after keep on trying I am able to make it work. I think its due to the CXFNonSpringJaxrsServlet. I made below changes and I was able to access the autogenerated WADL.
Changes to web.xml
<servlet>
<servlet-name>simplerest</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simplerest</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Added cxf-servlet.xml in WEB-INF
<jaxrs:server id="bookService" address="/bookservice">
<jaxrs:serviceBeans>
<ref bean="bs"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean='jsonProvider' />
</jaxrs:providers>
</jaxrs:server>
<bean id="bs" class="org.gsdev.ws.bookservice.BookResource"/>
<bean id="jsonProvider" class="org.gsdev.ws.bookservice.provider.XstreamJsonProvider"/>
Have you tried:
http://localhost:8080/SpringRestProjectJava/api/books?_WADL
By the way lots of goodies here:
http://cxf.apache.org/docs/jax-rs.html

Going from one view to another view in Spring, doesn't actually redirect - just adds the new view to the current view

So, I have a home page that goes to index.jsp. Which is fine. On this page there's a search button and is supposed to bring you to room-list.jsp but instead it simply gets embedded in to index.jsp and I can't understand why.
I've tried returning "redirect:/room-list" but that seems to be trying to go to another controller rather than a jsp if that makes sense? I'm so stuck. Here's a screenshot of what's happening, as you can see there's 2 banners since room-list.jsp is getting added on top of index.jsp: http://imgur.com/fJ3BA67
Here's what's in my controller:
#RequestMapping("/home")
public String home() {
System.out.println("in controller");
return "index";
}
#RequestMapping(value = "/search", method = RequestMethod.POST)
public String showMessage2(#Valid SearchCmd searchCmd, Model model) {
System.out.println("in search controller");
return "room-list";
}
Web.xml
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<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>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
dispatcher-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="com.programcreek" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Because you are using:
RequestMethod.POST
Try to change it to:
RequestMethod.GET
and you will get redirect to room-list page.
This tutorial may help: http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html
He's using
#RequestMapping(value = "/persons/edit", method = RequestMethod.GET)
To get the page first, then he uses
#RequestMapping(value = "/persons/edit", method = RequestMethod.POST)
To do business stuff.

Spring mvc doesn't see jsp

I'm creating a Spring MVC application and can't figure out why I get 404 error when launch my application. I want to see welcome page but it doesn't show. What is the issue?
In /WEB-INF/view there is index.jsp file with simple Hello text.
Web.xml
<?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_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>grun</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/grun-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>grun</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
grun-servlet.xml
<context:component-scan base-package="Controller"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
Controller
#Controller
#RequestMapping(value="/")
public class AController {
#RequestMapping(method=RequestMethod.GET)
public String welcome() {
return "index";
}
}
I think the problem is this below bit in grun-servlet.xml
base-package="Controller"
Unusual to name a package as Controller with the uppercase 'C'. So unless you have
package Controller;
at the beginning of your AController.java file, that is a problem.
You can also try the following
WEB-INF flolder in generally where runtime libraries and class go. Create view folder inside Webcontent folder and provide path as
<property name="prefix" value="/view/"/>
Wecontent is your root folder for all the resources you want to expose. Reason was doing above is that as Webcontent is the root folder to expose web resources directories inside it can be specified relative to this. So Any resource abc under folder xyz can be accessed using /abc/xyz.
Nothing is wrong with your configuration, Just add Index.jsp as a welcome file on you web.xml and it should works
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Please change your web.xml to this
<servlet>
<servlet-name>grun</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>grun</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/grun-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Hope it will work fine
2 possible things:
Did you try putting the request mapping at the method level instead of at the Controller level?
#Controller
public class AController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index";
}
}
Try adding the below to your grun-servlet.xml:
<context:annotation-config />
<context:component-scan base-package="com.xxx.yyy" />
where "com.xxx.yyy" is the base package to which your AController belongs to.

Servlet mapping URL is not calling in Spring MVC

I have spring project with web.xml configuration
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Here is my servlet-context.xml
resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**" />
<default-servlet-handler/>
<context:component-scan base-package="pk.training.basitMahmood.web.controller" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jspx" />
</beans:bean>
Here is my controller
#RequestMapping("/contacts")
#Controller
public class ContactController {
final Logger logger = LoggerFactory.getLogger(ContactController.class);
#Autowired
private ContactService contactService;
#RequestMapping(method = RequestMethod.GET)
public String list(Model uiModel) {
logger.info("Listing contacts");
List<Contact> contacts = contactService.findAll();
uiModel.addAttribute("contacts", contacts);
logger.info("No. of contacts: " + contacts.size());
return "contacts/list";
}
} //end of class ContactController
Now when i select run on server then i get the following page
But when i change the url to http://localhost:9090/ch17_i18nSupport/contacts then i get the error that
I have list.jspx in my contacts folder. Why i am getting not found error?
Thanks
Although this was solved by Basit in the comments, I've also added it as an answer:
No adapter for handler indicates that the #RequestMapping methods in your controller aren't being picked up. Do you have a <mvc:annotation-driven /> tag in your servlet-context.xml?

Categories