Java Spring address of page - spring

I am really confused how does the address of my spring application come from.
I followed the tutorial, but now I am trying to change the address, but I can't find the spot.
At the moment address is this : http://localhost:8080/HelloWorld/
Where can I change HelloWorld to something else?
I will provide you with any code you need. I have two conf files : web.xml, spring-servlet.xml, but there is nothing in them that contains HelloWorld.
<?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">
<context:component-scan base-package="hello.spring3.controller" />
<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>
</beans>
and 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_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="spring3" version="3.0">
<display-name>Spring display name</display-name>
<welcome-file-list>
<welcome-file>index.jsp</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>*.html</url-pattern>
</servlet-mapping>
</web-app>

Actually this is not issue unless you deploy it on production environament.
When you deploy it there you should create new HOST in TOMCAT_HOME/conf/server.xml file
it should look something like:
<Host name="www.mysite.com" appBase="www"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
Second option is to deploy your war file in ROOT directory, but in my opinion the first option is more suitable.
Update: No problem, If you are using eclipse, please click right mouse button on your project, Select Properties - > Web Project Settings.
and change name of your "HelloWorld" directory.

It's the name of your war file.

Related

Picking up Tomcat's Context.xml parameters via SpEL

Deploying war to Apache Tomcat 8 the following way.
Placing myApp.xml under $CATALINA_HOME/conf/[enginename]/[hostname]/
with the following contents:
<Context>
<Parameter name="myApp_configs" value="file:/the/path/to/configs/folder"
type="java.lang.String" override="false"/>
</Context>
B.t.w. I do not place any kind of Context.xml into war.
Then copying myApp.war to $CATALINA_HOME/webapps
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
</web-app>
And this way I try loading properties-file in beans.xml (referenced in web.xml above).
<?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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- Imported resources for cxf -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!--context:property-placeholder
location="#{contextParameters['myApp_configs']}/myApp.properties"/-->
<bean id="configurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="#{contextParameters['myApp_configs']}/myApp.properties"/>
</bean>
...other lines follow here...
</beans>
However I am getting following error upon beans loading:
Field or property 'contextParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Could you help me understand the error and propose a fix so that I can access Context-defined parameters?
P.S. I have not put here, but I also have some <Environment>-nodes in Context, and they are successfully accessible via JNDI in other places.
So as we were not able to solve the root cause - why contextParameters bean is not available, the following workaround (using ol' syntax) took place:
<bean id="myConfigsLocation"
class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
<property name="initParamName" value="myApp_configs" />
</bean>
<context:property-placeholder
location="#{myConfigsLocation}/myApp.properties" />
And it worked successfully.

How do I configure spring controller URLs to not have an extension?

My controllers are currently mapped to be something like http://example.com/fix.go and, of course, I think that's idiotic and want something nicer like http://example.com/fix or http://example.com/mmm/fix with no extension. When I try to configure this, however, I can't get it to work. I'm clearly missing a key part of understanding on the entire mapping. I'm using Spring 3.x, tomcat, and annotations for the controllers.
My web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>BooBoo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>BooBoo</servlet-name>
<url-pattern>*.*</url-pattern> <!-- was *.go when it worked -->
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
BooBoo-servlet.xml is:
<?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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<context:component-scan base-package="com.foofoo.booboo"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
And one of my controllers is configured as such:
#Controller
public class BangBangController extends BaseController {
// Used to be fix.go when it worked
#RequestMapping( value="fix", method=RequestMethod.GET )
public ModelAndView choose(
HttpSession session,
#RequestParam( value="fixId", required=false, defaultValue="-1" ) Integer fixId,
#RequestParam( value="forkId", required=false, defaultValue="-1" ) Integer forkId
)
throws Exception { ... }
}
I've tried changing the mapping in web.xml to /mmm/* expecting URLs like http://example.com/mmm/fix to work, but that too has not worked. I get "missing resource" errors when I enter what I think are the correct URLs in the browser.
What am I goofing up here? What critical piece of understanding do I lack? I've tried making the no extension thing work on another project at work, and couldn't get it to go there either. I'm obviously missing something.
The problem is your dispatcher servlet is matching anything with a "." in it.
Change it to
<url-pattern>/*</url-pattern>
However, that's also bad as you won't be able to serve static content.
What you really should do is this:
<url-pattern>/webapp/*</url-pattern>
Where "webapp" is some prefix. All of your URLs will need to be prefixed with it, but that allows you to still serve static content.
The answer was that I flubbed up the spring-servlet.xml file by not specifying that I was using the Spring MVC stuff. I appreciate Chris' help in diagnosing the issue. I needed confirmation that some of the things I tried (but failed because of this mistake) were correct.
I added
xmlns:mvc="http://www.springframework.org/schema/mvc"
and
<mvc:annotation-driven />
to that file, and boom, it started working. What leaves me scratching my head is, "How did it work without that?" Cuz it worked for *.go patterns.
Here's the resulting file in full:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<context:component-scan base-package="com.barbar.foofoo"/>
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<!--
Declare View Resolver: when view 'view_name' is called (from the Controller),
the file '/jsp/view_name.jsp' will be used.
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

What are the configurations or xml files to run a basic Spring MVC application?

I need to know the basic XML configuration files to put in my Web-inf folder, like web.XML, and where to write view resolver bean.
How do I register the controller?
When I run my application I get the error "could not open servlet context resource". What is servlet context resource and where do I mention it?
My web.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
jsp/index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
My springmvc is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/hello_world.html" class="springmvc.web.HelloWorldController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
I have these two xml files only.
I know I need a spring configuration file too. I don't know what details I need to put in that.
If you use STS (Spring Tool Suite) then you can create a Spring Template Project.
(New Project/SpringSource Tool Suite/Spring Template Project/-Spring MVC Project)
This contains all the files you need to start.

Tomcat unable to find jsp in war file

I recently switched one of my static html files to a Spring controller that uses a JSP to render its view. I use jetty to test locally and local testing shows the page rendering fine. Upon deploying to our test server, which uses Tomcat 6.0.26, I get the following exception:
javax.servlet.ServletException: Could not get RequestDispatcher for [/WEB-INF/jsp/index.jsp]: check that this file exists within your WAR
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:219)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have confirmed that the JSP exists in the war I deploy and the exploded directory that tomcat creates upon deployment. Here is what my web.xml and front-controller-servlet.xml files look like respectively (web.xml shorted slightly):
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- Initialize the Spring DispatcherServlet -->
<servlet>
<servlet-name>gwtrpc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map the DispatcherServlet to only intercept RPC requests -->
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>*.gwtrpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/search.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>front-controller</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
front-controller-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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/search.html">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="com.company.search.web.server.controller.IndexController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
The difference between my dev instance and test instance is that the dev instance is deployed to the root (localhost/search.html) whereas the test instance is deployed to server.com/appname/search.html. Appname is the name of the *.war file I deploy. I have tried adding the full path as the prefix to the jps files (/appname/WEB-INF/jsp/) and a number of other combinations with no luck. I have verified that the jsp-api jars are in the tomcat lib directory. My tomcat install is the basic/default install.
The issue was caused by the default jsp servlet being removed from the default web.xml of the tomcat install. This had been done by a former co-worker and not to my knowledge. Replacing the test server's web.xml with the default web.xml (which included url-patterns/servlets for handling jsp files) corrected the issue.

UrlFilenameViewController does not return View (Spring-MVC)

According to Spring framework API
UrlFilenameViewController's purpose is:
Transforms the virtual path of a URL into a view name and returns that view
If i request for /info.xhtml, its logical view name is info
See UrlFilenameViewController documentation
Both web.xml and controller is mapped according to
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8">
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>smac</display-name>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
/WEB-INF/controller-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="org.springframework.web.servlet.view.InternalViewResolver">
<property name="prefix" value="/WEB-INF/output/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</beans>
My smac app deploys fine.
So, if i request for http://127.0.0.1:8080/smac/info.xhtml, Spring-MVC should return /WEB-INF/output/info.jsp. But i have seen in console the following:
No mapping found for HTTP request with URI [/smac/info.xhtml] in DispatcherServlet with name 'controller'
Could anyone help me ?
regards,
You need to configure a HandlerMapping. Default one is BeanNameUrlHandlerMapping which will only do what you want if you bind your controller bean under the name matching your URI. You probably want the SimpleUrlHandlerMapping instead:
<bean name="myController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/**/*.xhtml=myController
</value>
</property>
</bean>

Resources