Using Spring3.2 MVC with embedded jetty9 - spring-mvc

I am trying to implement an embedded jetty server using spring mvc for dispatching. I tried several tutorials but monstyl they are out of date or don't use embedded jetty.
So far I created a new spring mvc project from scratch in intellij idea.
src/
main/
java/
com.springapp.mvc/
HelloController.java
Launcher.java
webapp/
WEB-INF/
mvc-dispatcher-servlet.xml
web.xml
pom.xml
HelloController.java
#Controller
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
}
Launcher.java
public class Launcher {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
//todo: implementation to use dispatcher-servlet...
server.start();
server.join();
}
}
mvc-dispatcher-servlet.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"
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">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<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>Spring MVC Application</display-name>
<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>
</web-app>
How do I tell the server correctly to use the dispatcher servlet?
I tried a few things out but nothing really worked yet.

The solution for everyone who is also interested... (in scala not in java)
val server: Server = new Server(8080)
private val root: WebAppContext = new WebAppContext
root.setContextPath("/")
root.setDescriptor("src/main/webapp/web.xml")
root.setResourceBase("src/main/webapp/"))
root.setParentLoaderPriority(true)
server.setHandler(root)
server.start()
server.join()

Related

Spring MVC Form Validation not validating

Trying to apply MVC Spring Validation to my web project. I think I have everything configured properly, but my form is not being validated.
I am not using Maven or Gradle. Rather, I am including the jar files I was told I needed by my tutorial in my build path.
The jar files are:
validation-api-1.1.0.Final.jar
hibernate-validator-5.0.1.Final.jar
I am following this tutorial: https://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api
My config.xml 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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven />
</beans>
My Model:
package bl;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class VendorValidation {
#NotEmpty
#Size(min = 1, message = "Field requires an entry")
private String vname;
#NotEmpty
private String vphone;
#NotEmpty
private String vemail;
#NotEmpty
private String vcity;
#NotEmpty
private String vstate;
#NotEmpty
private String vcountry;
#NotEmpty
private String vzipcode;
#NotEmpty
private String vtimezone;
public String getVname() {
return vname;
}
public void setVname(String vname) {
this.vname = vname;
}
public String getVphone() {
return vphone;
}
public void setVphone(String vphone) {
this.vphone = vphone;
}
public String getVemail() {
return vemail;
}
public void setVemail(String vemail) {
this.vemail = vemail;
}
public String getVcity() {
return vcity;
}
public void setVcity(String vcity) {
this.vcity = vcity;
}
public String getVstate() {
return vstate;
}
public void setVstate(String vstate) {
this.vstate = vstate;
}
public String getVcountry() {
return vcountry;
}
public void setVcountry(String vcountry) {
this.vcountry = vcountry;
}
public String getVzipcode() {
return vzipcode;
}
public void setVzipcode(String vzipcode) {
this.vzipcode = vzipcode;
}
public String getVtimezone() {
return vtimezone;
}
public void setVtimezone(String vtimezone) {
this.vtimezone = vtimezone;
}
}
My View:
<form:form method="post" id="va-form" action="insertVendor" modelAttribute="vendorValidation">
<div class="form-group">
<label>Vendor Name</label> <form:input type="text" path="vname" class="form-control"
id="nameForm" />
</div>
...
</form:form>
My Controller:
#RequestMapping(value = "vendorForm", method = RequestMethod.GET)
public String formView(ModelMap map, HttpServletRequest request) {
VendorValidation vendorValidation = new VendorValidation();
map.put("vendorValidation", vendorValidation);
return "vendorForm";
}
RequestMapping(value = "/insertVendor", method = RequestMethod.POST)
public String insertVendor(#Valid #ModelAttribute("vendorValidation") VendorValidation vendorValidation, BindingResult result,
HttpServletRequest request, ModelMap model) {
System.out.println("Found form errors: " + result.hasErrors());
if (result.hasErrors())
{
return "vendorForm";
}
else
{
// database logic
return "vendormanagement";
}
}
The BindingResult object does not contain any errors when attempting to submit the form and it should when I leave my fields empty. So the if (result.hasErrors()) does not fire and I get a database exception for trying to insert null values.
Download the Spring framework "-dist.zip" from here - https://repo.spring.io/release/org/springframework/spring/
Download hibernate ORM zip - https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.4.2.Final/hibernate-release-5.4.2.Final.zip/download
Add jar from directory - hibernate-release-5.4.2.Final\lib\required
Download hibernate validator - https://sourceforge.net/projects/hibernate/files/hibernate-validator/6.0.16.Final/hibernate-validator-6.0.16.Final-dist.zip/download
Add jars present outside : hibernate-validator-6.0.16.Final\dist
Remember : Add all the jars to the classpath.
Now, I'm providing a sample code to demonstrate :
spring.xml or application-context.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">
<context:component-scan
base-package="com.demo" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Add custom message resources -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="resources/message"></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://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>project</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
I think that this will solve your problem.

SpringMVC error to run jsp

I have a problem with the following code.
When I run mobile/menu it works, but when I run mobile/Home, with a
jsp in WEB-INF/views/Menu.jsp, it doesn't work.
Can anyone help me to find the mistake?
Thanks in advance.
My Controller
#RestController
#RequestMapping(value="/mobile")
public class UsersController {
#Autowired
UsersService userServices;
#RequestMapping(value="/menu", method=RequestMethod.POST, headers="Accept=application/json")
public #ResponseBody Map<String,Object> getMenu(mng_menu menu){
Map<String,Object> map = new HashMap<String,Object>();
List<mng_menu> list = userServices.listaMenu();
if (list != null){
map.put("status","200");
map.put("message","Data found");
map.put("data", list);
}else{
map.put("status","404");
map.put("message","Data not found");
}
return map;
}
#RequestMapping(value="/Home", method = RequestMethod.GET, headers="Accept=application/json")
public ModelAndView Home(){
ModelAndView view =new ModelAndView("Menu");
return view;
}
My SpringMVC-servlet
<?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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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>SpringMVC</display-name>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/mobile/Home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/mobile/menu</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Swagger2 config class not loaded in spring mvc

I am working on setting up swagger2 documentation for my Spring REST Project. But when I try to execute http://localhost:8085/swagger-ui.html returns empty page.The problem is my bean class is not loaded by my spring MVC application and it is NOT a spring boot application.
The below is my swagger2 config class
#Configuration
#EnableSwagger2
#EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter{
#Bean
public Docket newsApi() {
System.out.println("!#################################################22");
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Dialr")
.apiInfo(apiInfo())
.select()
.paths(regex("/*.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring REST Sample with Swagger")
.description("Spring REST Sample with Swagger")
.termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
.contact("Niklas Heidloff")
.license("Apache License Version 2.0")
.licenseUrl("https://github.com/IBM-Bluemix/news-aggregator/blob/master/LICENSE")
.version("2.0")
.build();
}
}
The below is my rest-servlet.xml under WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.xx.yy.zz" />
<mvc:annotation-driven />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
<context:annotation-config/>
<bean name="swaggerConfig" class="com.xx.yy.SwaggerConfig"/>
</beans>
Please find the web.xml entry as well
<?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>Retail_SVC</display-name>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/rest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Please let me know if anything is missing ? Any help is highly appreciated.
Since the base url mapping in your servlet mapping is /rest/* the swagger-ui.html will not load to http://localhost:8085/swagger-ui.html so
try with http://localhost:8085/rest/swagger-ui.html it may work
I used the WebMvcConfigurer instead of the WebMvcConfigurerAdapter because that class is already deprecated.
#Configuration
#EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
#Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("Spring Boot Swagger App")
.description("\"Spring Boot Swagger Server App\"")
.version("1.0.0")
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
.build();
}
public ApiInfo apiInfo() {
final ApiInfoBuilder builder = new ApiInfoBuilder();
builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
.description("The API provides a platform to query build test swagger api");
return builder.build();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

404 error while using multiactioncontroller by resource not found in Spring2,5

All the configurations are allmostproper in code but misplacement of resources is going on
From "simple.jsp" the request /student3.htm is sending to " web.xml" and to "spring-servlet.xml" to SimpleUrlHandlerMapping request /student3.htm is mapped with bean id=class3 it enters to multiactioncontroller having methodNameParameter has property.
And it is wired to PropertiesMethodNameResolver class where its property is "mappings" maps the request /student3.htm to "save" named method in multiactioncontroller extended class there is "Multi.java" class.
Then save method returns ModelAndView as view name "multi" for multi.jsp through InternalResourceViewResolver in /WEB-INF/jsp/multi.jsp for message multiactionsuccess.
HTTP Status 404 - /student3.htm
--------------------------------------------------------------------------------
type Status report
message /student3.htm
description The requested resource (/student3.htm) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.35
In console
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jdk1.6.0_12\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Java\jdk1.6.0_12\bin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:springmvc2.56' did not find a matching property.
no error message is found and only warning message.please found out the 404 error found in Broswer.
In 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">
<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>*.htm</url-pattern>
</servlet-mapping>
</web-app>
in 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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/student3.htm=class3
/student4.htm=class3
/student5.htm=class3
/student6.htm=class3
</value>
</property>
</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/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="class3" class="com.spring.controller.Multi">
<property name="methodNameResolver" >
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings" >
<value>
/student3.htm=save
/student4.htm=update
/student5.htm=delete
/student6.htm=select
</value>
</property>
</bean>
</property>
</bean>
</beans>
In WebContent simple.jsp
<%#taglib prefix="c" uri="http://www.springframework.org/tags" %>
<%#taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<a href="/student3.htm" >save</a>
<a href="/student4.htm" >update</a>
<a href="/student5.htm" >delete</a>
<a href="/student6.htm" >select</a>
In com.spring.controller package Multi.java is controller Class
package com.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class Multi extends MultiActionController
{
protected ModelAndView save(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception
{
ModelAndView mav = new ModelAndView();
mav.setViewName("multi1");
return mav;
}
protected ModelAndView update(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception
{
ModelAndView mav = new ModelAndView();
mav.setViewName("multi1");
return mav;
}
protected ModelAndView select(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception
{
ModelAndView mav = new ModelAndView();
mav.setViewName("multi1");
return mav;
}
protected ModelAndView delete(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception
{
ModelAndView mav = new ModelAndView();
mav.setViewName("multi1");
return mav;
}
}
In WEB-INF/jsp folder "multi.jsp" is present
multiaction success.
method - access specifies should be "public" by userdefined methods
-returntype should be "ModelAndView,Map,void"
In the above code retifications are
In WebContent simple.jsp
<a href="student3.htm" >save</a>
<a href="student4.htm" >update</a>
<a href="student5.htm" >delete</a>
<a href="student6.htm" >select</a>
In com.spring.controller package Multi.java is controller Class
method access specifies should be public
change multi1 to multi as successView in all methods
After editing the code done this is successfully executed.

spring mvc 3 No mapping found for HTTP request with URI

Error info: No mapping found for HTTP request with URI [/TestSpringWebApp/hello.htm]
Any help would be greatly appreciated!
I am using annotation to map request to controller.
controller code :
#Controller
#RequestMapping("/hello.htm")
public class HelloController {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}
Dispatcher-servlet.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<!-- <bean name="/hello.htm" class="com.kibboko.poprocks.web.HelloController"/>-->
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
<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 also need to add
<mvc:annotation-driven />
to your Dispatcher-servlet.xml file.
You have to add #RequestMapping to the method, not to the class. If you add it to the class it will be applied as a prefix to all the methods mapping of this class, but you also have to map the methods of the class.
You can have #RequestMapping for class also, but every method also should have its own " #RequestMapping" please see below example.
#Controller
#RequestMapping("/appointments")
public class AppointmentsController {
private final AppointmentBook appointmentBook;
#Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
this.appointmentBook = appointmentBook;
}
#RequestMapping(method = RequestMethod.GET)
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
#RequestMapping(value="/{day}", method = RequestMethod.GET)
public Map<String, Appointment> getForDay(#PathVariable #DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
return appointmentBook.getAppointmentsForDay(day);
}
#RequestMapping(value="/new", method = RequestMethod.GET)
public AppointmentForm getNewForm() {
return new AppointmentForm();
}
#RequestMapping(method = RequestMethod.POST)
public String add(#Valid AppointmentForm appointment, BindingResult result) {
if (result.hasErrors()) {
return "appointments/new";
}
appointmentBook.addAppointment(appointment);
return "redirect:/appointments";
}
}

Resources