My application's folder structure is as follows.
web/WEB-INF/templates/
-home.ftl
web/resources/css/Home_files
-test.css
When using both <mvc:annotation-driven /> and <mvc:resources mapping="/resources/**" location="/resources/css/Home_files" /> tags it couldn't resolve view (http://localhost:8080/info/home/index.html).
without <mvc:resources mapping="/resources/**" location="/resources/css/Home_files" /> tag view is resolved but images ans css couldn't resolve.
without <mvc:annotation-driven /> tag view could not be resolved but images and css could be resolved.
How do i load both view and static content together?
here is my config xml files and homeController.
info-servlet.xml (configuration file)
<?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/cache
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/templates/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
</bean>
<context:component-scan base-package="com.test.web.controllers"/>
<context:component-scan base-package="com.test"/>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/css/Home_files" />
</beans>
controller
#Controller
#RequestMapping("/home")
public class HomeController {
#RequestMapping(value = "/index.html")
public String getHome(#ModelAttribute("model") ModelMap model) {
return "home";
}
}
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_2_5.xsd" >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/info-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>info</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/info-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>info</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
It seems that path is not correct in <mvc:resources> tag. Add a forward slash (/) at the end of location.
Instead of
<mvc:resources mapping="/resources/**" location="/resources/css/Home_files" />
Use this:
<mvc:resources mapping="/resources/**" location="/resources/css/Home_files/" />
Related
How I can fix errors PWC6033: Error in Javac compilation for JSP||PWC6199: Generated servlet error: in my webapp on Spring MVC with jetty server. I get this error when I try open web .jsp page in browser. I try to fix this issue adding in file idea64.exe.vmoptions string
"-Dorg.apache.jasper.compiler.disablejsr199=true", but this don't give positive result.
my project below
Launcher.java
`
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import java.net.URL;
import java.security.ProtectionDomain;
public class Launcher {
public static void main(String[] args) throws Exception {
Server server = new Server(8189);
ProtectionDomain domain = Launcher.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
server.setHandler(webapp);
server.start();
server.join();
}
}
`
MainController.java
` import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class MainController {
#RequestMapping("/")
public String showHomePage() {
return "index";
}
}
<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">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="ru.gb" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Spring MVC Jetty Server Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
beans.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">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="ru.gb" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
Im trying to deploy a Spring MVC web app.
I want to redirect my index.jsp to another .jsp but i get this error.
HTTP Status 404 – Not Found
--------------------------------------------------------------------------------
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
My project Struture:
src/main/java
com.example.beans
com.example.controller
com.example.dao
src/main/webapp/
WEB-INF
/jsp
test.jsp
spring-servlet.xml
web.xml
index.jsp
spring-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.xsd">
<!-- Provide support for component scanning -->
<context:component-scan
base-package="com.example />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**"
location="/resources/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/dbexample></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="dao" class="com.example.dao.ObjectDao">
<property name="template" ref="jt"></property>
</bean>
</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"
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">
<display-name>Example</display-name>
<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>
</web-app>
Controller
#Controller
public class MainController {
#RequestMapping("hello")
public String redirect() {
return "viewpage";
}
#RequestMapping("/")
public String display() {
return "index";
}
#RequestMapping("test")
public String test() {
return "test";
}
index.jsp
<html>
<body>
<h2>Hello World!</h2>
View test
Click here...
</body>
</html>
When i clik on test I get this error in the sts log:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /example/hello
You should use '/' for mapping for your controllers.
You could try this:
#RequestMapping("/example/hello")
public String redirect() {
return "viewpage";
}
And it's better to use relative path for links:
View test
Click here...
More about pageContext see this
first time build a springmvc web project,use jetty as web container,but when jetty started, but when request the url:"http://localhost:8080/hello/mvc" the
broswer report:
"Problem accessing /hello/mvc. Reason: ".And the console report:"十一月 18, 2018 10:34:16 上午 org.springframework.web.servlet.PageNotFound noHandlerFound
警告: No mapping found for HTTP request with URI [/hello/mvc] in DispatcherServlet with name 'mvc-dispatcher'
"
my web.xml:
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--param-name>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-name-->
<param-value>classpath*:spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
mvc-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: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">
<!-- scan the package and the sub package -->
<context:annotation-config/>
<context:component-scan base-package="com.zhangbing.springmvc">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name ="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsps/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>`enter code here`
</beans>
The hierarch of my project:
enter image description here
circled by blue line is the controller
The contoller helloMvcController:
package com.zhangbing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/hello")
public class helloMvcController {
#RequestMapping("/mvc")
public String helloMvc()
{
return "home";
}
i search lots question like this,but no one can solved my problem,please help.
I believe, Spring can not find your controller.The problem might be with the component scan.
Please change your component scan directive from
<context:component-scan base-package="com.zhangbing.springmvc">
to
<context:component-scan base-package="com.zhangbing.springmvc.*">
Then lets see what is the output.
I have a small issue in request mapping,
I have controllerA with request mapping as
#Controller
#RequestMapping(value={"login/formA.html", "B/formB.html", "C/formC.html"})
public class ControllerA {
}
I need to create one more controller without disturbing the existing controller,
request mapping for secound controller like below
#Controller
#RequestMapping(value={"X/test1", "Y/test2", "Z/test3"})
public class ControllerB {
}
How can I configure the spring-servlet.xml to configure the above scenario and to make it work.
I have the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="https://java.sun.com/xml/ns/j2ee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>/public/login/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/public/signout/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/public/X/*</url-pattern>
</servlet-mapping>
</web-app>
My spring-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"
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">
<mvc:annotation-driven/>
<context:component-scan
base-package="com.abc.xyz.controller"/>
<context:component-scan
base-package="com.abc.xyz.controller.example1"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class='org.springframework.web.servlet.view.ContentNegotiatingViewResolver' p:order='1'>
<property name='mediaTypes'>
<map>
<entry key='json' value='application/json' />
<entry key='html' value='text/html' />
</map>
</property>
<property name='viewResolvers'>
<list>
<bean class='org.springframework.web.servlet.view.BeanNameViewResolver' />
<bean 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>
</list>
</property>
</bean>
Plese do give me suggestion to resolve this issue , struck with few days.
Thanks in Advance.
Add one more servlet mapping in web.xml like given below
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/public/*/*</url-pattern>
</servlet-mapping>
I am using context:component-scan, still the dependencies are not getting injected.
Here is my setup. ConnectionHelper property is not getting injected in the LoginController class.
WEB-INF/web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>vd</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>vd</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
WEB-INF/applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<context:component-scan base-package="com.example" />
</beans>
WEB-INF/vd-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<bean class="com.example.interceptors.SslInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/login" />
<bean class="com.example.interceptors.SslInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
</beans>
com.example.controllers.LoginController.java
#Controller
public class LoginController {
#Inject
private ConnectionHelper connectionHelper; //this is null after loading
}
com.example.connection.ConnectionHelper.java
#Component
public class ConnectionHelper {
}
Please tell me what's wrong here...after hours of troubleshooting i can't determine the problem.
Update
I changed my configuration and moved everything to vd-servlet.xml.
Quite miraculously I have found that #Autowired works (Injects Dependencies) with this configuration but #Inject does not.
And <mvc:annotation-driven /> is still required even if you use <context:component-scan />, otherwise #RequestMapping is not detected.
vd-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.example" />
<mvc:interceptors>
<bean class="com.example.interceptors.SslInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/login" />
<bean class="com.example.interceptors.SslInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
</beans>
Agree with #Alex, just that the file where you are missing <context:component-scan/> or <context:annotation-config/> is not applicationContext.xml file but vd-servlet.xml file.
Either <context:annnotation-config/> or <context:component-scan/> will register a AutowiredAnnotationBeanPostProcessor responsible for handling #Autowired, #Resource, #Inject annotations
I believe that your applicationContext.xml is missing:
<context:annotation-config>
this registers Springs post processors for annotation based configuration.
You need to add getters and setters to the LoginController. Another option is to make connectionHelper a protected variable. This is how Spring "sees" the property to be injected.
In LoginController:
#Inject
private ConnectionHelper connectionHelper;
public ConnectionHelper getConnectionHelper() {
return connectionHelper;
}
public void setConnectionHelper(ConnectionHelper connectionHelper) {
this.connectionHelper = connectionHelper;
}