Spring MVC app not running, throwing 404 when accessing - java

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

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

.html file is not working in spring3

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.

Spring MVC request Mapping Class level

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>

Json doesn't output anything

I am very new to spring framwework and building rest web services.. I have a problem in outputing JSON from my ws project. Here the source code.
Controller
#RestController
public class Controller {
#RequestMapping(value = "/sample/{name}", method = RequestMethod.GET)
public #ResponseBody
String sample(#PathVariable String name) {
String temp = name;
return temp;
}
}
web. xml
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<!-- set forceEncoding to true if you want to override encoding of servlet -->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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-context.xml
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<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.package.package" />
In my pom.xml, I already have a jackson for JSON.
When i try to run "localhost:8081/projectName/param".. It didn't output anything. Am I missing a piece of my code. Please help.

Spring with no view resolver but plain html

I'm trying to configure Spring to use only my html files instead of jsp view resolver but can't get it to work. I tried many different configurations and I simply want to have redirection to /WEB-INF/views/index.html everytime localhost:8080/ is entered. Now what I have in my tomcat console is:
org.springframework.web.servlet.PageNotFound - No mapping found for
HTTP request with URI [/WEB-INF/views/index.html] in DispatcherServlet
with name 'appServlet'.
here's my servlet-context.xml snippet.
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="WEB-INF/views/" />
<beans:property name="suffix" value="" />
</beans:bean>
<view-controller path="/" view-name="index.html"/>
Any suggestions what am I missing?
EDIT-
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 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>
<welcome-file-list>
<welcome-file>/WEB-INF/views/index.html</welcome-file>
</welcome-file-list>
I know this is an older question so this is for anyone who comes here now. Spring is auto-configured to pick up your views as long as your file paths are correct, so you should not have to do any spring webmvc configuration.
I ran into this issue in Intellij and simply had to restart the application and the error was gone. Hope someone sees this and doesn't spend hours working on this.
This error
org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/views/index.html] in DispatcherServlet with name 'appServlet'.
is telling us that you are actually going to the following address
localhost:8080/WEB-INF/views/index.html
which obviously makes no sense.
If you want
to have redirection to /WEB-INF/views/index.html everytime
localhost:8080/ is entered
add a welcome file to your deployment descriptor.
<welcome-file-list>
<welcome-file>WEB-INF/views/index.html</welcome-file>
</welcome-file-list>
Try to add a / to the prefix.
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value="" />
</beans:bean>
<view-controller path="/" view-name="index.html"/>

Categories