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/
Related
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>
I use spring mvc (4.0) and I want to use a servlet to intercept the root mapping ("/") instead of the dispatcherservlet. Spring has a configuration, called "default-servlet-name". The documentation says the following:
The name of the default Servlet to forward to for static resource requests. The handler will try to auto-detect the container's default Servlet at startup time using a list of known names. If the default Servlet cannot be detected because of using an unknown container or because it has been manually configured, the servlet name must be set explicitly.
The problem with the above configuration is that the default servlet is called on every request.
Web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.company.main.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
spring-servlet-config.xml:
<mvc:annotation-driven/>
<context:annotation-config />
<mvc:default-servlet-handler default-servlet-name="index" />
<context:component-scan base-package="com.company.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/jsp/"
p:suffix=".jsp"
p:requestContextAttribute="rc" />
So, with the above configuration, the indexServlet functions as root path. But is called multiple times, because it is the default-handler. If I remove the tag "default-servlet-handler" from the spring config, the page won't load. Any workaround for this?
Thanks in advance!
The problem is that root mapping / can only by mapped by the default servlet (Java EE sense).
So IMHO, you have only one clean way to meet your requirement : you map spring dispatcher-servlet to /, you do not map IndexServlet and have spring forward to it for / URL.
You could use a ServletForwardingController for that :
In web.xml :
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.company.main.IndexServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In spring-servlet-config.xml:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
/=indexForwardingController
</property>
</bean>
<bean id="indexForwardingController" class="org.springframework.web.servlet.mvc.ServletForwardingController">
<property name="servletName"><value>index</value></property>
</bean>
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.
My web.xml contains
<?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" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
resources/index.html
</welcome-file>
</welcome-file-list>
</web-app>
The resources/index.html references to other static resources like images, js, css etc. stored in resoruces directory by relative paths.
When I put http://localhost/MyProject/ in browser, it showed up index.html but didn't get the css and javascripts.
However, if I put http://localhost/MyProject/resources/index.html in the browser, everything shows up correctly.
So, the question is how can I let the welcome page served in the url as the path given in the <welcome-file>, e.g. /resources/index.html.
If it can't be done in the <welcome-file list>, what other configurable method should I use.
I tend not to redirect to /resources/index.html by adding another html or by doing it programmatically in a Servlet controller.
It seems you are using Spring and having problems with static content.
Try looking at this link
It explains how to proceed in this case...
Pay atention to the line:
<mvc:resources mapping="/resources/**" location="/resources/" />
It maps your resources folder (containing the css, javascript and image files) to a special handler of Spring.
Update:
In your servlet-context.xml file you can add this line
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Resolves view names to protected .html resources within the /WEB-INF/resources/ directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/resources/"/>
<property name="suffix" value=".html"/>
</bean>
that says that you don't have to use the 'index.jsp' properly. This way, you will map a view to the "/" access. Summarizing, this way the user enters in 'http://localhost/MyProject/' and sees your index.html and sees the effects of css and javascripts.
PS.: - This configurations only works on Spring 3+
- Prefer naming your files to '.jsp' and not '.html'... it is simpler to map.
My project is using seam 2.2.2 Final, and the application server is Jboss 7.1. After a long deployment process, finally I can deploy it successfully. But when I attempt to open it in the browser, I get error like this:
"This webpage has a redirect loop
The webpage at "http://localhost:8080/integration/debug.seam?cid=2" has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer." .
How can I solve this problem?
This is my 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>integration</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.seam</url-pattern>
</servlet-mapping>
<context-param>
<param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
<param-value>Mojarra-1.2</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>
<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>
<filter>
<filter-name>Seam Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Seam Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Seam Resource Servlet</servlet-name>
<url-pattern>/seam/resource/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<security-constraint>
<display-name>Restrict raw XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
</web-app>
This is my persistence.xml. I think configuration of persistence.xml is correct.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="integration" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/integrationDSJNDI</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/integrationEntityManagerFactory"/>
</properties>
</persistence-unit>
</persistence>
Jar list
antlr-2.7.6.jar
antlr-runtime-3.1.1.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
drools-api-5.0.1.jar
drools-core-5.0.1.jar
gwt-servlet-2.3.0.jar
hibernate-core-3.3.0.SP1.jar
hibernate-validator-3.1.0.GA.jar
drools-compiler-5.0.1.jar
itext-2.1.2.jar
itext-rtf-2.1.2.jar
jboss-el-1.0_02.CR5.jar
jboss-seam-2.2.1.Final.jar
jboss-seam-debug-2.2.1.Final.jar
jboss-seam-ioc-2.2.1.Final.jar
jboss-seam-jul-2.2.1.Final.jar
jboss-seam-mail-2.2.1.Final.jar
jboss-seam-pdf-2.2.1.Final.jar
jboss-seam-remoting-2.2.1.Final.jar
jboss-seam-ui-2.2.1.Final.jar
jbpm-jpdl-3.2.2.jar
json-20080701.jar
jta-1.1.jar
mvel2-2.0.10.jar
richfaces-api-3.3.3.Final.jar
richfaces-impl-3.3.3.Final.jar
richfaces-ui-3.3.3.Final.jar
xml-apis-1.0.b2.jar
xstream-1.3.1.jar
This problem caused by lack of jsf-facelet dependency. to solved that adding the following code to your pom.xml
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.15</version>
</dependency>
Use solution 1 https://community.jboss.org/thread/206184
I resolved this issue copying the debug.xhtml from jboss-seam-debug.jar to root web src path.
Use solution 2 https://issues.jboss.org/browse/JBSEAM-4864
moving of META-INF/debug.xhtml to META-INF/resources/debug.xhtml should help to solve the exception.
Fixed it in jboss-seam-debug.jar 2.3.0