.html file is not working in spring3 - java

I am developing spring mvc application using spring3.0
my web.xml file is as below
Spring.xml
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>/</url-pattern>
</servlet-mapping>
spring-servelet.xml
<context:component-scan
base-package="com.asset" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
when I run my project it give 404 resource not found exception.
but If I use index.jsp as welcome page its work and index page is appear.
So can you Please comment why .html is not working as welcome page.

Related

Spring advice is getting called in tomcat but not working in jboss

I have a spring project in which i am applying an aspect before RequestMapping Annotation within Controller Class. i have tested the same in tomcat there was no issue but while i am writing the same code in jboss aop call is getting skipped.
the expression for pointcut is :
#Pointcut(" within(#org.springframework.stereotype.Controller *) && #annotation(requestMapping) && execution(public * *(..))")
web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config/springreport-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
springreport-servlet.xml
<context:component-scan base-package="com.*"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:default-servlet-handler />
<aop:aspectj-autoproxy proxy-target-class="true"/>

Spring MVC Static Resource Sometimes Can't Find

First, Sorry about my poor english
=================================
My static resource in '/resource/images/test1/image1.png'
When "test1" folder is exist
I can call '127.0.0.1:8080/myproject/resource/test1/image1.png' find it
if i call '127.0.0.1:8080/myproject/resource/test1/image1.png' when folder not exist yet
of course i can't find it
but even i call url when after dynamically create folder
i still can't got image
EX1:
Call url before create folder and call again after dynamically create folder
------ Can't find, Only Get 404 Not Found
EX2:
Create folder before call url and call again
------ Can find
I thought it was a cache problem at first
But when EX1 problem occur, stall occur even change browser
Both EX are deployment on linux
I have the following Setting
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/base-config.xml</param-value>
</context-param>
<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>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>signin</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
........
........
........
<mvc:annotation-driven />
<context:component-scan
base-package="com.chat.controller" />
<mvc:resources mapping="/resource/**" location="/resource/" cache-period="0"/>
<!-- jsp views -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

How to fix 404 error, The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

When I am trying to execute #RequestMapping("/showForm") I am facing an error.
I think my code seems fine, I am simply returning new String with the name of my JSP file - "mainmenu.jsp". I have this folder the folder JSP in the right place.
The error:
Description The origin server did not find a current representation
for the target resource or is not willing to disclose that one
exists.
Where can be the problem?
<mvc:annotation-driven />
<context:component-scan
base-package="com.crunchify.controller" />
<mvc:default-servlet-handler />
<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>
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/mainmenu.jsp</url-pattern>
<url-pattern>/mainmenu.html</url-pattern>
<url-pattern>/processForm.jsp</url-pattern>
<url-pattern>/processForm.html</url-pattern>
<url-pattern>/index.jsp</url-pattern>
<url-pattern>/main-menu.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
The actual request:
#RequestMapping("/showForm")
public String helloWorld() {
return "mainmenu";
}
Your deployment descriptor(web.xml) does not have mapping for the url you are trying to access. Either add "/showForm" in url mapping for dispatcher servlet or use a wild card "/" in your url mapping for your dispatcher servlet. like,
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
As web.xml is the entry point, there should be url mapping.
(And also you can map different url for different dispatcher servlet. In other dispatcher servlet you can use another view resolver.)
Hope <url-pattern>/</url-pattern> it will work for you.
Go to your apache web server config, its probably in ProgramFiles > Apache Tomcat folder
Go to tomcat>conf folder
Edit server.xml
Search "Connector port"
Look for the connection timeout, if its set to -1 then that is the issue
Change it to "20000", save the file. Restart the Apache Service
Problem will be fixed.

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" />

Cant access view through direct URL [Maven]

I'm creating my first project in Maven, and already have a noob problem.
In my WEB-INF folder, I have another folder called "classes", in that folder I have a view called "view.jsp".
When I try to access the URL
http://localhost:8080/ProjectenIIJavaWeb/classes/view
I get a Http 404 not found error.
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<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>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>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
You have configured the DispatcherServlet only for the url pattern *.htm. So you have to call a URL with that pattern, otherwise there is no servlet which matches your request.
As the name says, the folder "classes" is for Java classes. Resources should be in the root of the WAR file.
You don't call your view, you define request mappings and let Spring MVC handle the view resolution. I suggest to read the Spring MVC documentation (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc)
In case you plan to keep your jsp's(without exposing the detailed path of actual file) inside WEB-INF this should help
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Detailed example: http://www.mkyong.com/spring-mvc/spring-mvc-internalresourceviewresolver-example/

Categories