Spring servlet between jsp and html - java

I wanna know how to servlet call html and jsp file page.
I'm using spring mvc with servlet
when I run
localhost:8080/ -> it's running correctly
localhost:8080/htmlPage -> it's not working return error 404 not found
Here my path
Here my code
servlet mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=""/>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven />
web.xml
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
HelloController.java
#Controller
public class HelloController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String wellcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello.jsp";
}
#RequestMapping(value="/htmlPage", method = RequestMethod.GET )
public String startHtml(){
return "hello.html";
}
}

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.so</url-pattern>
</servlet-mapping>
#Controller
public class HelloController {
#RequestMapping(value = "/home.so", method = RequestMethod.GET)
public String wellcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello.jsp";
}
#RequestMapping(value="/htmlPage.so", method = RequestMethod.GET )
public String startHtml(){
return "hello.html";
}
}
please change your url pattern with ".so". all ".so" request are consider as spring request and handle by spring dispatcher servlet.

Your call is wrong. First there should be an app name and then request name.
You have to call like below :
localhost:8080/YourAppName/htmlPage

Related

SpringMVC HTTP Status 404 -

I'm starting to learn Spring MVC, but when I run my program, I am getting the following error:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.41
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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/springmvc-servlet.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>
</web-app>
springmvc-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
SpringTest.java:
package com;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/home")
public class SpringTest {
#RequestMapping(value = "/home",method =RequestMethod.GET)
public String home(){
return "home";
}
}
My project structure is as follows:
I use IntelliJ IDEA to run this procedure. Will the IntelliJ IDEA be the reason for the error I'm getting?
Please to change the order between <context:component-scan base-package="com"/> and <mvc:annotation-driven/> make <mvc:annotation-driven/> the first
You are trying to access ${ContextPath}/home/home, please try to replace your controller by below code:
#Controller
public class SpringTest {
#RequestMapping(value = "/home",method =RequestMethod.GET)
public String home(){
return "home";
}
}

404 error for Spring MVC Rest URI

I am making a dummy hello world applicatiion for Spring MVC, i have done exactly same as was mentioned in example, but when i run the URL, its giving me the 404 error. Please help me if you can, following are my files:-
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<mvc:default-servlet-handler default-servlet-name="moqahServices"/>
<servlet>
<servlet-name>moqahServices</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>moqahServices</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
</web-app>
moqahServices.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.moqah" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloController.jsp
package com.moqah;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
#Controller
#RequestMapping("/hello")
public class HelloController{
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Please let me know if you have any suggestions
i guess the mvc:annotation-driven element should be used in your moqahServices, check it out
Looking into your code it seems,
1) you configured mvc:default-servlet-handler in the wrong place. It should be the part of your spring configurations.
2) In web.xml considering the servlet-name as moqahServices, spring configuration name should be suffixed with -servlet.xml followed by servlet-name ie. your spring configuration name is expected as moqahServices-servlet.xml
Please Try doing above changes.

Running Java Servlet cant view welcome page 404 error

Having an issue like this.
Error 404--Not Found
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.5 404 Not Found
web.xml
Here is my ..\MTSM\src\main\webapp\WEB-INF\web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ActionAdet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ActionSure</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispacher-servlet.xml
Here is my ..\MTSM\src\main\webapp\WEB-INF\mvc-dispacher-servlet.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
hello.jsp
Here is my ..\MTSM\src\main\webapp\WEB-INF\pages\hello.jsp
$.get('ActionAdet', {opername: 'services'}, function (responseJson) { } ) ;..
hello.HelloController.java
Here is my ..\MTSM\src\main\java\com\springapp\mvc\HelloController.java
#Controller
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "ss";
}
}
ActionAdet.java
Here is my ..\MTSM\src\main\java\com\springapp\mvc\ActionAdet.java
#Controller
#RequestMapping("/ActionAdet")
public class ActionAdet extends HttpServlet { } etc..
I can't view my project in this url: localhost:7001/MTSM/
and also not available in: localhost:7001/MTSM/hello.jsp
It was working 2 days ago. I think I'm deleted something in web.xml, but I don't remember.
Do you know what problem is?
I uploaded my project here: [Dropbox -- MTSM_20140112.zip][3]
Thanks.
According to the logs you provided in pastie (that's bad : you should have providen the relevant part as an edit), spring tried to load WEB-INF/pages/ss.jsp and did not find it. I means :
you probably hit the controller HelloController
the method printWelcome returned ss as a string
due to mvc-dispacher-servlet.xml, InternalResourceViewResolver looked for WEB-INF/pages/ss.jsp (normal untill here)
as the file does not exist you got a 404 error (still normal : it is not what you expected, but what you asked)
You should try to be coherent between view names returned by controller and actual jsp file :
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
}
should forward to view WEB-INF/pages/hello.jsp (that exists according to you project image)

Spring MVC 3.0- The URL mapping is treated as a view when passing a model and view object

I am new to spring and was working on the Hello world application in MVC The URL mapping is searched as a corresponding view when i return the ModelAndView object from the controller..I have included all proper jars.This is the code..
/web-inf/sample-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"> (http://www.springframework.org/schema/context/spring-context-3.0.xsd%27%3E)
<context:component-scan base-package="com.tcs.laks.sample.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
This is the web.xml---
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> (http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%27%3E)
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The HelloWorldController is..
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
#Controller
#RequestMapping("/welcome")
public class HelloWorldController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
String message = "Hello";
return new ModelAndView("hello", "message", message);
}
}
The url was given as //localhost:8080/sample/welcome it gave 404 as it was trying to find welcome.jsp instead of hello.jsp
HTTP Status 404 - /sample/WEB-INF/jsp/welcome.jsp
type Status report
message /sample/WEB-INF/jsp/welcome.jsp
description The requested resource (/sample/WEB-INF/jsp/welcome.jsp) is not available.
Specify the contextConfigLocation init-param for the dispatcher servlet within your web.xml file. The current configuration is even being read/used by the dispatcher
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/sample-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Also it looks like you have imported the portlet version of ModelAndView, make sure this is what you desire. If not import, org.springframework.web.servlet.ModelAndView
You are using wrong ModelAndView class
import org.springframework.web.portlet.ModelAndView;
it should have been
import org.springframework.web.servlet.ModelAndView;
everything else is fine.

No mapping found in DispatcherServlet without using extension

I am receiving the message "The requested resource () is not available." when going to a url that I have mapped using DispatcherServlet. It previously worked when I had the mapping setup to use a .do extension, but when I map without an extension it doesn't work.
Controller:
#Controller
public class WodServiceController {
#Autowired
private WodService wodService;
#RequestMapping("/json/wod")
#ResponseBody
public Word getWord(HttpServletRequest request, HttpServletResponse response) {
return wodService.getWordOfTheDay();
}
}
dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="foobar.controller" />
<!-- View Resolvers -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Services</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:commonBeans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/json/*</url-pattern>
</servlet-mapping>
</web-app>
If I have the mapping set to *.do and the RequestMapping set to wod.do it works fine.
EDIT:
In the logs, when I go to that URL it shows the message
No mapping found for HTTP request with URI [/foobar/json/wod] in DispatcherServlet with name 'dispatcher'
EDIT 2: I added the alwaysUseFullPath to the handler:
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
This was due to the way my apache redirect was setup.

Categories