Spring <mvc:resources mapping="/css/**" location="/css/" /> doesn't work - spring

Hi mapping css doesn't work.
This is my config:
spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package="spring.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/content/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<display-name>JewishProjectT</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>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>
and my heder file:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<c:choose>
<c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18en'}">
<c:set var="approach" value="left" scope="page"></c:set>
</c:when>
<c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18ru'}">
<c:set var="approach" value="left" scope="page"></c:set>
</c:when>
<c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18he'}">
<c:set var="approach" value="right" scope="page"></c:set>
</c:when>
<c:otherwise>
<c:set var="approach" value="left"></c:set>
</c:otherwise>
</c:choose>
<link rel="stylesheet" media="all" type="text/css" href="/css/common.css">
<link rel="stylesheet" media="all" type="text/css" href="/css/lavamenu.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/<c:out value="${approach}"/>/lavalamp.js"></script>
<title>${title}</title>
</head>
<body>
<div id="main">
in chrome it shows:
/js/left/lavalamp.js 404 (Not Found) 0:19 GET
/css/common.css 404 (Not Found) 0:17 GET
/css/lavamenu.css 404 (Not Found)
How to correctly configure paths to css/images/js?
P.S. I excluded all links which starts with http:\\ , because rules of stackoverflow do not permit me post them as I am beginner.

Your <mvc:resources mapping="/css/**" location="/css/" /> seems fine to me, but are you sure your application is running in the root? Shouldn't your URLs start with some other context, like:
href="/myapp/css/common.css"
Or, to avoid hardcoded references:
href="<c:url value="/css/common.css" />"
If your application is running in the root, then you might need the correct namespace declarations? Something like:
<?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/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">

Related

what is wrong with my Spring MVC 3.0 configuration?

i am a newbie in the Java EE Spring MVC coding area. When i configured my first Spring MVC 3.0 site, i got a strange question that i have to manually type the MVC named url route to make it work.
The complete url route in my example is:
http://localhost:8080/SpringMVC/hello.jsp
i wanted to send a word to the controller and display it on the view.
But when i click return, the error page said:
HTTP Status 404 - /hello.do
type Status report
message /hello.do
description The requested resource is not available.
Apache Tomcat/7.0.85
So the url route was then: http://localhost:8080/hello.do
And i have to type the complete route :
http://localhost:8080/SpringMVC/hello.do to make it work.
i think there must be some errors in my web.xml and SpringMVC-servlet.xml configurations. i post all of my code below and welcome any suggestions.
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>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>*.do</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 配置上传文件的参数 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>
<!-- 配置Controller -->
<bean name="/hello.do" class="com.yyy.controller.HelloController"></bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
HelloController.java:
package com.yyy.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloController extends AbstractController{
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String hello = request.getParameter("hello");
System.out.println("------:" + hello);
ModelAndView mav = new ModelAndView("index");
mav.addObject("helloworld", "hello "+hello);
return mav;
}
}
hello.jsp
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="hello.do" method="post">
hello:<input type="text" name="hello"/>
<input type="submit" value="submit">
</form>
</body>
</html>
And the index.jsp:
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>index.jsp</title>
</head>
<body>
<h1>${helloworld} </h1>
</body>
</html>
You can change the <form action> like below. This is a better approach.
<form action="${pageContext.request.contextPath}/hello.do">

Spring-security-3 browser back button issue

I am just trying to learn Spring security 3. while running example of Spring security the back button takes me to previous page . I want to stop this. I just try to do that using spring security.but it was not resolved,Please help.Here is my code
Security 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:security="http://www.springframework.org/schema/security"
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.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<mvc:annotation-driven />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*" />
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0" />
<property name="useExpiresHeader" value="true" />
<property name="useCacheControlHeader" value="true" />
<property name="useCacheControlNoStore" value="true" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
<security:user-service id="userServiceDAO">
<security:user name="mukesh" authorities="ROLE_USER"
password="password" />
</security:user-service>
<security:authentication-manager>
<security:authentication-provider
user-service-ref="userServiceDAO" />
</security:authentication-manager>
<security:http auto-config="false">
<security:form-login login-page="/login"
login-processing-url="/secure/sayHello" username-parameter="_username"
password-parameter="_password" authentication-failure-url="/error"
default-target-url="/secure/defaultTarget" />
<security:intercept-url pattern="/login"
access="IS_AUTHENTICATED_ANONYMOUSLY" />
<security:intercept-url pattern="/secure/**"
access="ROLE_USER" />
<security:logout logout-url="/logout" />
</security:http>
</beans>
FrontController-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:c="http://www.springframework.org/schema/c" 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.2.xsd
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-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="sample.security" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp">
</bean>
</beans>
MVC-controller
package sample.security.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class SecureLoginController {
#RequestMapping(value = {"/","/login"}, method = RequestMethod.GET)
public String secureLogin() {
return "login";
}
#RequestMapping(value = "/secure/defaultTarget", method = RequestMethod.GET)
public String goToIndexPage(#RequestBody String body) {
System.out.println("Request body is :"+ body);
return "success";
}
#RequestMapping(value = {"/error"}, method = RequestMethod.GET)
public String goToAgainLogin() {
return "error";
}
}
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<h2>Please Login</h2>
<c:url value="secure/sayHello" var="loginURL" />
<form action="${loginURL}" method="post">
<label for="username">User Name</label> <input
type="text" size="30" name="_username" id="username"><br /></br> <label
for="password">Password</label> <input
type="password" size="30" name="_password" id="password"><br /></br> <input
type="submit" value="Submit">
</form>
</body>
</html>
success.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>success</title>
</head>
<body>
<h2>I got success</h2>
</body>
</html>
error.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error page</title>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
</head>
<body>
<h2>Invalid use name Or password</h2>
<c:url value="secure/sayHello" var="loginURL" />
<form action="${loginURL}" method="post">
<label for="username">User Name</label> <input
type="text" size="30" name="_username" id="username"><br /></br>
<label for="password">Password</label> <input
type="password" size="30" name="_password" id="password"><br /></br>
<input type="submit" value="Submit">
</form>
</body>
</html>
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_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/configuration/CustomSecurity.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>
<servlet>
<servlet-name>FrontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configuration/FrontController-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
please provide me a Solution to rectify this issue.Thanks in advance
Let Spring Security set a default set of security-related headers:
<security:http auto-config="false">
<security:headers />
<!-- other stuff ... -->
</security:http>
Note that this will actually not stop the user to go back to the previous page, but the browser will be told not to cache it.

How I will enter into web flow from any point in Spring MVC web app? Spring Web Flow webapp is not working

I have been trying to develop an multi-page user registration form using Spring Web Flow but could not completed. Later on I am going to paste my application code in this post.
I would be grateful to one who identify the missing part or error and guide me to resolve the same.
My webapp name is 'UserRegistrationSWF'. Here is the directory structure:
UserRegistrationSWF
-Java Resources
-src
-org.nitest.controller
-UserRegistrationController.java
-org.nitesh.model
-User.java
-WebContent
-WEB-INF
-config
-swf-config.xml
-web-application-config.xml
-swf
-swf-flow.xml
-userRegistrationPage2.jsp
-userRegistrationLastPage.jsp
-view
-cancel.jsp
-success.jsp
-userRegistrationStartPage.jsp
-web.xml
-index.jsp
I am using Spring MVC and my welcome page is 'index.jsp'. Here is the code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
User Registration
</body>
</html>
Here is 'web.xml' code:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>UserRegistrationSWF</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>User Registration</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>User Registration</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
Here is the 'web-application-config.xml' code:
<?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:webflow="http://www.springframework.org/schema/webflow-config"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="org.nitesh" />
<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/view/" />
<property name="suffix" value=".jsp" />
</bean>
<import resource="swf-config.xml"/>
</beans>
Here is the 'swf-config.xml' code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<webflow:flow-registry id="flowRegistry" base-path="/WEB-INF">
<webflow:flow-location path="/swf/swf-flow.xml" />
</webflow:flow-registry>
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
</webflow:flow-executor>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry"/>
</bean>
</beans>
Here is the 'swf-flow.xml' code:
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">
<view-state id="userRegistrationStartPage" view="userRegistrationStartPage.htm" model="user">
<transition on="cancel" to="cancel"></transition>
<transition on="proceed" to="userRegistrationPage2"></transition>
</view-state>
<view-state id="userRegistrationPage2" view="userRegistrationPage2.htm">
<transition on="revise" to="userRegistrationStartPage"></transition>
<transition on="proceed" to="userRegistrationLastPage"></transition>
<transition on="cancel" to="cancel"></transition>
</view-state>
<view-state id="userRegistrationLastPage" view="userRegistrationLastPage.htm">
<transition on="revise" to="userRegistrationPage2"></transition>
<transition on="confirm" to="success"></transition>
<transition on="cancel" to="cancel"></transition>
</view-state>
<end-state id="success" view="swf/success.jsp"></end-state>
<end-state id="cancel" view="swf/cancel.jsp"></end-state>
</flow>
Here is the 'userRegistrationStartPage.jsp' code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Registration Start Page</title>
</head>
<body>
<form:form commandName="user" method="POST">
<table>
<tr>
<td>Name:</td>
<td><form:input path="name"/></td>
</tr>
<tr>
<td>Email:</td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="password"/></td>
</tr>
<tr>
<td><input type="submit" name="_eventId_cancel" value="Cancel"></td>
<td><input type="submit" name="_eventId_proceed" value="Next"></td>
</tr>
</table>
</form:form>
</body>
</html>
The 'cancel.jsp' and 'success.jsp' simply prints cancel and success message respectively.
The 'User' class is a userRegistrationStartPage form bean class.
The 'userRegistrationPage2.jsp' and 'userRegistrationLastPage.jsp' Simply prints messgage for now.
Here is web app controller 'UserRegistrationController.java' code:
package org.nitesh.controller;
import org.nitesh.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class UserRegistrationController {
#RequestMapping(value = "userRegistrationStartPage.htm")
public ModelAndView showUserRegistrationFormStartPage(#ModelAttribute("user") User user)
{
return new ModelAndView("userRegistrationStartPage");
}
}
Preceding is the web app codes.
Now I would like to come to my questions:
1. How I will enter into web flow from any point in Spring MVC web app? I mean how I will take entry into web flow by clicking following link in 'index.jsp':
User Registration
Above mentioned webapp is not working, what more is missing that do I need to add or change in order to make it working?
I am really looking forward to hear the answer from one.
Thanks.
You're almost there:
It's a best practice to add all elements of your flow in one folder. So move the first page of your flow named 'userRegistrationStartPage.jsp' from /view/ to /swf/
-WebContent
-WEB-INF
-config
-swf-config.xml
-web-application-config.xml
-swf
-swf-flow.xml
-userRegistrationStartPage.jsp
-userRegistrationPage2.jsp
-userRegistrationLastPage.jsp
-view
-cancel.jsp
-success.jsp
-web.xml
-index.jsp
Because you defined a flow in your swf-config.xml
<webflow:flow-registry id="flowRegistry" base-path="/WEB-INF">
<webflow:flow-location path="/swf/swf-flow.xml" />
</webflow:flow-registry>
The mapping to start that flow will be swf (the first part of the path)
Change your index.jsp to the following
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
User Registration
</body>
</html>
Also, because you're using a User object to store the registration information, you need to create one at the start of the flow. See the the booking-mvc example of spring webflow where a Hotel object is needed at the start of the flow:
<flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<input name="hotelId" required="true" />
<on-start>
<evaluate expression="bookingService.createBooking(hotelId, currentUser.name)" result="flowScope.booking" />
</on-start>
<view-state id="enterBookingDetails" model="booking">
etc...
</view-state>
</flow>
Here they use a service to get a hotel from the database, YMMV.
Lastly, the UserRegistrationController isn't needed, so you can remove it.
Update
The quickest way to create a new User object at the start of your flow would be to add the following line at the beginning of swf-flow.xml:
<on-start>
<evaluate expression="new org.nitesh.model.User()" result="flowScope.user" />
</on-start>

CSS problems from No mapping found for HTTP request with URI warning

I am having an issue with resolving the path of my CSS files whenever I add anything to my RequestMapping path. In the example below, if I change
#RequestMapping(value="/articles/view", method = RequestMethod.GET)
to
#RequestMapping(value="/articles", method = RequestMethod.GET)
it works fine. I'm also having problems mapping my css files if I attempt to use path variables. My css files are sitting in a folder called /data/css where /data is at the same level as my /WEB-INF.
ArticlesAction.java:
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
#Controller
public class ArticlesAction {
#RequestMapping(value="/articles/view", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
//model.addAttribute("id", id);
return "articles";
}
}
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" version="2.5">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
mvc-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test.actions" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
articles.jsp
<!DOCTYPE html>
<html class="no-js" lang="en-US">
<!-- Head section -->
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="description" content="Responsive website template by kubasto" />
<meta name="msapplication-TileImage" content="data/img/tile/w.png" />
<meta name="msapplication-TileColor" content="#089bc3" />
<link rel="shortcut icon" href="data/img/favicon/w.png" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Rokkitt:400,700" />
<link rel="stylesheet" href="data/css/fancybox.css" />
<link rel="stylesheet" href="data/css/flexslider.css" />
<link rel="stylesheet" href="data/css/mejs.css" />
<link rel="stylesheet" href="data/css/tipsy.css" />
<link rel="stylesheet" href="data/css/base.css" />
<link rel="stylesheet" href="data/css/structure.css" />
<link rel="stylesheet" href="data/css/parts.css" />
<link rel="stylesheet" href="data/css/widgets.css" />
<link rel="stylesheet" href="data/css/bright.css" />
<link rel="stylesheet" href="data/css/color.css" />
Try to use special mvc:resources tag:
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
To use it add "mvc" namespace to your mvc-dispatcher-servlet.xml

maven and spring mvc is not working?

I'm trying to build an application using Spring MVC and Maven 3.0 within Eclipse using the webapp. I'm not able to get to the initial page, or navigate to any other pages from there w/out getting a 404 error. Please let me know if there's something I'm missing. Thanks!
mvc-dispatcher.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="se.guards.controller" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<context:annotation-config />
<!-- show pictures -->
<mvc:default-servlet-handler />
<!-- also add the following beans to get rid of some exceptions -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mymessages</value>
</list>
</property>
</bean>
web.xml
<web-app id="WebApp_ID" 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>Spring MVC Application</display-name>
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
/WEB-INF/spring-database.xml,
</param-value>
</context-param>
<!-- Spring Security -->
<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>
database.xml
<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-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
userController.java
package se.guards.controller;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import se.datalayer.guards.service.UserService;
import se.guard.User;
#Controller
#RequestMapping(value="/")
public class UserController
{
#SuppressWarnings("unused")
#Autowired
private UserService userService;
#RequestMapping(value = "/", method = RequestMethod.GET)
public String findAllUsers(Model model){
Collection<User> userList= new ArrayList<User>();
model.addAttribute("users", userList);
return "showallusers";
}
}
showallusers.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4 /loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>found users</title>
</head>
<body>
<table>
<c:forEach var="allusers" items="${users}">
<tr>
<td>${allusers.firstname}</td>
<td>${allusers.lastname}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Answer to the questions:
This is the code I'm using.
The #Controller annotation must be there.
As I said before I get :
HTTP Status 404 - /guard_weblayer/ type Status report message /guard_weblayer/
description The requested resource (/guard_weblayer/) is not available.
Apache Tomcat/7.0.25
I use a restfule design, a domainlayer, datalayer and weblayer. Can it effecting the weblayer?
You might want to start over and use an example from springbyexample. It contains everything you need in a understandable fashion (and its in maven as well.)
Most likely that's easier than debugging the current code.

Resources