how to direct default page to an spring controller? - spring-mvc

The project(named:'myproject') use Spring MVC framework. The default page will be "index.action", web.xml configed as below:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.action</welcome-file>
</welcome-file-list>
The server response 404 not found When while visiting http://www.example.com/myproject/, but it works fine while visiting http://www.example.com/myproject/index.action.
Any help will be appreciated!

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Changing the servlet mapping to the above means all request mappings will handled by spring.

Related

Servlet Mapping With Wildcards

I'm having trouble with the servlet mapping in web.xml. It works when I put in a specific file but not when using a wildcard:
<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>/resources123/bootstrap.css</url-pattern>
</servlet-mapping>
This works fine. But when I change the URL pattern to /resources123/* it doesn't hit the DispatcherServlet.

Spring Web MVC tomcat deployment issues

I am developing one Spring Web application with Intellij IDEA deployed to tomcat.
I met the issues below:
When I set the application context to the application name(my app) in the Run/Debug configuration page then run tomcat i can access the tomcat homepage and can see "myapp" in the manager page but can't access it.
The URL will be "localhost:8080/request" when clicking myapp in the manager page.
I have to add "myapp" to the URL as "localhost:8080/myapp/request".In this way the requested page can be accessed.
But if i removed "/myapp" from application context leaving it blank in the Run/Debug configuration page then run tomcat i will access the requested page successfully.
The URL is "localhost:8080/request" now the same as before without "myapp".
If I just added the myapp.war file in /webapps folder the result is the same as the first scenario.
What i want is that I put the war file in /webapps folder and start the tomcat server then the page can be accessed.
Anything wrong i have done for the configuration or missed something?
Thanks in advance for the help.
This is the web.xml
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<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>
<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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Swagger geneates urls not with suffix defined by url-pattern of servlet in the web.xml

Here is my web.xml:
<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/web-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
but, when I try it out in Swagger-ui, it sends a request having a url without suffix '.do', surely 404 returns, how can I config it?
One way to fix it is to do some changes in the js of Swagger-ui, but it's not a good way.

Servlet Mappings Jetty 8

I have a webapp that works with Tomcat 8 and Jetty 8. Recently, we have introduced servlet-mappings
<servlet>
<servlet-name>page</servlet-name>
<jsp-file>/Dashboard.html</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>page</servlet-name>
<url-pattern>/editjob</url-pattern>
</servlet-mapping>
When you put an url with "http://localhost:8080/app/editjob", in tomcat is redirected to "http://localhost:8080/app/Dashboard.html". But when we deploy it using embed Jetty, a 404 Not Found it is being returned. It seems that the servlet-mapping is not working.
HTTP ERROR 404
Problem accessing /app/page. Reason:
Not Found
Powered by Jetty://
I can enter in the app by using directly
http://localhost:8080/app/Dashboard.html. Without mappings, we have no problem with jetty
This is the code for embedding Jetty:
final Server server = new Server();
SocketConnector connector = new SocketConnector();
connector.setMaxIdleTime(-1);
connector.setSoLingerTime(-1);
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
final WebAppContext context = new WebAppContext();
context.setServer(server);
context.setContextPath("app");
context.setWar(location.toExternalForm());
server.setHandler(context);
context.start();
server.join();
This is the web.xml with the mappings.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<display-name>app</display-name>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/Login.html</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>page</servlet-name>
<jsp-file>/Dashboard.html</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowSubdomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE, OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Origin, Content-Type, X-Requested-With, Access-Control-Allow-Origin</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>page</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>page</servlet-name>
<url-pattern>/jobs</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Index.html</welcome-file>
</welcome-file-list>
</web-app>
I have tried to define the servlet-mappings when i start the server, but did not work.
This mappings are working perfectly with Tomcat, but Jetty...I dont know which could be the problem.
Thanks in advance.
Problem accessing /app/page.
That URL is not mapped anywhere in your pasted web.xml
The servlets with urls patterns (defined in <servlet-mapping> entries) are:
/webresources/*
/login
/home
/jobs
None of those will match on a request URL of /page, resulting in the DefaultServlet (which is default mapped at /) returning a 404 for that requested resource.
Also, you haven't setup JSP in your embedded-jetty environment, so no JSP is being used.
Copied from prior answer https://stackoverflow.com/a/28483349/775715
If that code snippet represents how you are initializing your webapp, then you are missing a lot of JSP initialization steps.
There's an example project produced by the Jetty Project showing how to use JSP with embedded Jetty at
https://github.com/jetty-project/embedded-jetty-jsp
Pay attention to ...
The dependencies you need
The required ClassLoader type
The scratchDir declaration
The ServletContainerInitializer setup
The Jsp Servlet Holder and mappings
The InstanceManager
(just to name a few big ones)

Multiple JAX-RS applications

I have specified my JAX-RS Application in the web.xml as follows:
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>
com.myapp.MYRestApplication
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Is it possible for me to pass another JAX-RS application class to this CXF servlet?
Regards,
Anand
While CXF can manage multiple JAX-RS applications within a single servlet — I use this configuration in my own code — it is sufficiently complex that you're far better off switching to using a Spring-based configuration. The CXFNonSpringJaxrsServlet can only support limited configuration. In my code, I have this (a little complex because I'm also using Spring security, but you can drop that filter):
<!-- Location of the main Spring/CXF configuration file -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<!-- Apply Spring Security as a wrapper to everything -->
<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>
<!-- Hook for Spring lifecycle management; you want this -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- A single servlet to handle all CXF processing -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Resources