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";
}
}
Related
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.
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>
I define two spring context files for my application, springmvc-config.xml for the springMVC and han-config.xml for the root Application Context.
What I want to do is to use springmvc-config.xml to scan all the #Controller beans and han-config.xml to scan all the #Component, #Repository, and #Service beans
My problem is if I use the <context:component-scan base-package="com.leo.han" / > in the springmvc-config.xml the application will run perfectly and no errors during the deployment.
But if I want to use
<context:component-scan base-package="com.leo.han.controllers" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
in the springmvc-config.xml to only scan the controllers and use
<context:component-scan base-package="com.leo.han" use-default-filters="false">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
in the han-config.xml to only scan for the #Component, #Repository, and #Service beans which is a recommended approach in the spring forum.
I will get terrible errors like
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.leo.han.services.UserService com.leo.han.controllers.LoginController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.leo.han.services.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
it seems like the userService bean is never created for some reason
Can anyone help me on this
My package structure is:
com.leo.han.controllers (all controllers annotated with #Controller)
com.leo.han.services (all service annotated with #Service)
com.leo.han.dao (all das annotated with #Repository)
Here is a sample code
#Controller
public class LoginController {
#Autowired
private UserService userService;
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLogin() {
return "login";
}
#RequestMapping(value = "/newaccount", method = RequestMethod.POST)
public String newAccountPost(Model model, User user) throws Exception {
user.setEnabled(true);
user.setAuthority("APP_VIEW");
userService.addUser(user);
return "accountcreated";
}
}
#Service("userService")
public class UserService {
#Autowired
private UserDao userDao;
public List<User> getAllUsers() {
return userDao.searchAll();
}
public void addUser(User user) {
userDao.createUser(user);
}
public boolean isUserExist(User user) {
return userDao.isUserExist(user);
}
}
My web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/han-config.xml
</param-value>
</context-param>
<filter>
<display-name>springSecurityFilterChain</display-name>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
My springmvc-config.xml (springMVC context config file)
<context:component-scan base-package="com.leo.han.controllers" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsps/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="1"></property>
</bean>
<bean id="viewTileResolver"
class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property name="order" value="0"></property>
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/layouts/layout.xml</value>
</list>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/message" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"></property>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language"></property>
</bean>
</mvc:interceptors>
my han-config.xml (my root applicationContext file)
<context:component-scan base-package="com.leo.han"
use-default-filters="false">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<import resource="persistence-config.xml" />
<import resource="security-config.xml" />
Please help, I have already search around here for two days in order to find a solution to my case, however all the solution seems to not work for my case.
Thank you
I am using similar configuration but the base package is same "a.b" in both cases and one contains exclusion for Controllers ( for root context) and other consists of inclusion ( for mvc context) and it works fine. You can try that.
Now you'd love Spring for this - If you want to programmatically determine the candidate beans in the classpath with your base package, you can write a simple test program like below which will list the candidate components.
public class TestComponentScanner {
public static void main(String[] args) {
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(true);
String basePackage = "com/leo";
provider.addExcludeFilter(new AnnotationTypeFilter(Controller.class, true));
Set<BeanDefinition> filteredComponents = provider.findCandidateComponents(basePackage);
System.out.println("No of components :"+filteredComponents.size());
for (BeanDefinition component : filteredComponents) {
System.out.println("Component:"+ component.getBeanClassName());
}
provider.resetFilters(true);
provider.addIncludeFilter(new AnnotationTypeFilter(Controller.class, true));
filteredComponents = provider.findCandidateComponents(basePackage);
System.out.println("No of components :"+filteredComponents.size());
for (BeanDefinition component : filteredComponents) {
System.out.println("Component:"+ component.getBeanClassName());
}
}
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.
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()