i am using maven, spring 3 for dynamic project. I am getting error at <bean name="/hello.cs" class="com.maven.controller.HelloController"/> as Multiple annotations found at this line:
- Class 'com.maven.controller.HelloController'
Project Structure
Web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>hello</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>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.cs</url-pattern>
</servlet-mapping>
</web-app>
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="/hello.cs" class="com.maven.controller.HelloController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
index.jsp
<jsp:forward page="WEB-INF/jsp/home.jsp"></jsp:forward>
home.jsp
<form action="./hello.cs" method="post">
<div>
<div>
User Name:<input type="text" name="name"/>
</div>
<div>
Password: <input type="password" name="pass"/>
</div>
<div>
<input type="submit" value="Login"/>
</div>
<div>
<p style="color: red">${msg}</p>
</div>
</div>
</form>
HelloController
package com.maven.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController extends AbstractController{
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String name = req.getParameter("name");
String pwd = req.getParameter("pass");
Map<String, String> m = new HashMap<String, String>();
if(name.equalsIgnoreCase("xyz") && pwd.equalsIgnoreCase("123")){
m.put("msg", "welcome mr. "+req.getParameter("name"));
return new ModelAndView("success", m);
} else {
m.put("msg", "Invalid");
return new ModelAndView("home");
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javatpoint</groupId>
<artifactId>CubeGeneratorWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>CubeGeneratorWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>CubeGeneratorWeb</finalName>
</build>
</project>
Error:
EDIT: It says class 'com.maven.controller.HelloController' not found.
I see that you have put your com.maven.controller.HelloController.java file under src\main\resources folder.
It should be under src\main\java folder. So the hierarchy should be like
src
|-main
|-java
|-com
|-maven
|-controller
|-HelloController.java
The reason is the directory structure followed by maven detects java files only if it's placed under this hierarchy src\main\java, if you are using default configuration.
Can you right click on project and go to Properties > Java Build Path > Source and check whether src\main\java entry is there in the build path list or not? If not add it and try again.
After lots of struggle i got the answer, i had error in spring.xml file. check the solutions.
i need to add below code only in spring.xml file.
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
previously i was using below code
<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">
for above code i need to change pom.xml file, i mean need to add more dependency.
so this is my final spring.xml file for this question only.
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="/hello.sa" class="com.maven.controller.HelloController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Related
This question already has answers here:
Problems with rendering JSP in spring boot
(3 answers)
Closed 2 years ago.
Just trying to create a simple Spring MVC application using maven,but getting this error(No mapping found for request with URL).Tried every possible solutions provided before for these type of error but not getting the correct result.Any help will be highly appreciate .Thank you in advance.
I will try to implement the view resolver part later,here simply trying to show one jsp page after clicking on the submit button on the index.jsp page using spring mvc.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.subu</groupId>
<artifactId>Demo_SpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Demo_SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>Demo_SpringMVC</finalName>
</build>
</project>
index.jsp
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>subu</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>subu</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Dispatcher-Servlet named as subu-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.subu.*"></ctx:component-scan>
</beans>
The controller class named as AddController.java:
package com.subu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddController {
#RequestMapping("/add")
public String add()
{
return "display.jsp";
}
}
Project directory is like this:
Not sure it will completely resolve your issue but I see some config issue.
1. Spring is searching for mapping with URL /Demo_SpringMVC/add. Its picking base URL from pom.xml finalName
Do any 1 of following
<servlet-mapping>
<servlet-name>subu</servlet-name>
<url-pattern>/Demo_SpringMVC/*</url-pattern>
</servlet-mapping>
or Map at Controller level like this
#Controller
#RequestMapping("/Demo_SpringMVC")
public class AddController {
#RequestMapping("/add")
public String add()
{
return "display.jsp";
}
}
Doing above will at least trigger add() method. Verify it by adding a sysout.
2. I don't see any View Resolver related configuration. Better to add one, something like below. Also, add JSP's inside WEB-INF folder.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
as per your question, error is given below,
No mapping found for HTTP request with URI [/Demo_SpringMVC/add] in
DispatcherServlet with name 'subu'
reason for this error is, not configured the InternalResourceViewResolver in your subu-servlet.xml file. it used to map URI to actual URI. for more info, spring documentation, A Guide to the ViewResolver in Spring MVC
modify the subu-servlet.xml file as following,
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.subu.*"></ctx:component-scan>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
Best Practice
create a folder named "jsp" under the "WEB-INF" folder and move all .jsp files into "jsp" folder. (above answer is based on this tip)
Ok i got it.I was getting the no mapping error due to this line in dispatcher servlet:
<ctx:component-scan base-package="com.subu.*"></ctx:component-scan>
after removing the * from base-package able to get the correct output expected.
<ctx:component-scan base-package="com.subu"></ctx:component-scan>
Regarding the view resolver part,i have not implemented it and still getting the correct output from the display.jsp page.i think View resolver is not required here as i am providing the display.jsp page to the controller myself.
I am learning spring MVC. This is a basic code which I found online and trying to run locally. I have added all the dependencies but I am still getting this error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Here is the code:
This is the landing page which contains the form. On submit, controller will be called.
index.jsp
<html>
<body>
<form action="hello">
UserName : <input type="text" name="name"/> <br><br>
Password : <input type="text" name="pass"/> <br><br>
<input type="submit" name="submit">
</form>
</body>
</html>
This is the maven xml which has the spring dependencies.
pom.xml
Added the below two dependencies:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
This is the controller. Login is successful when password is 'admin'.
HelloController.java
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloController {
#RequestMapping("/hello")
public String display(HttpServletRequest req,Model m)
{
//read the provided form data
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(pass.equals("admin"))
{
String msg="Hello "+ name;
//add a message to the model
m.addAttribute("message", msg);
return "viewpage";
}
else
{
String msg="Sorry "+ name+". You entered an incorrect password";
m.addAttribute("message", msg);
return "errorpage";
}
}
}
web.xml
<web-app>
<display-name>SpringMVC</display-name>
<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>
</web-app>
spring-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"
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">
<!-- Provide support for component scanning -->
<context:component-scan base-package="com.javatpoint" />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
viewpage.jsp
<html>
<body>
${message}
</body>
</html>
errorpage.jsp
<html>
<body>
${message}
<br><br>
<jsp:include page="/index.jsp"></jsp:include>
</body>
</html>
In your web.xml <context:component-scan base-package="com.javatpoint" />,but I don't see package com.javatpoint; in your HelloController.You need to create a package and put your contorller in it and set it up in web.xml.
Try to create com.javatpoint package and put your controller in it.
And you also set <property name="prefix" value="/WEB-INF/jsp/"></property> you need to put your viewpage.jsp and errorpage.jsp in /WEB-INF/jsp. I don't konw whether you create jsp directory in WEB-INF.
Sometimes controller can't find your jsp cause 404.
I create a project using your code,I work well.
I'm starting learning the Spring MVC. Want to make web app with Excel file upload to database.
I'm starting with simple file upload and got problems at beginning.
My project made in Netbans using Maven and Tomcat 8.
I made simple example project with tutorial
https://saarapakarinen.wordpress.com/2015/01/11/building-a-basic-spring-3-web-project-netbeans-and-maven/
and it worked perfectly, but I wanted to change it a little to file upload based on official Spring help
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multipart
I got opening form with file upload, when I select file and submit it i got an error:
HTTP Status 500 - Could not parse multipart servlet request; nested exception is
java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
How to solve this and where is error? It is error in my code or I'm missing somthing, or got wrong configuration?
I have one form on form.jsp file and one controller class HelloController why its not working?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- name of the project//-->
<display-name>HelloProject</display-name>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/application/*</url-pattern>
</servlet-mapping>
<!-- max time of the session //-->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- default page //-->
<welcome-file-list>
<welcome-file>application/form.jsp</welcome-file>
</welcome-file-list>
</web-app>
front-controller-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- configuration to fetch jsp files automatically from WEB-INF/jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1000000"/>
</bean>
<context:component-scan base-package="helloweb"/>
</beans>
form.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form Page</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="">
<label>file to send: <input type="file" name="file" /></label>
<input type="submit" />
</form>
</body>
</html>
HelloController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package helloweb;
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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
#Controller
public class HelloController
{
private String startmessage="start";
#RequestMapping("form")
public String viewLoginPage(Model model)
{
model.addAttribute("message", startmessage);
return "form";
}
#RequestMapping(value = "form", method = RequestMethod.POST)
public String login(
#RequestParam(value = "file", required = true) MultipartFile file)
{
if((file.isEmpty()) )
{
startmessage="file empty";
return "redirect:form";
}
else
{
byte[] bytes = file.getBytes();
return "hello";
}
}
}
Same error even adding #ModelAttributeI am not able to get the model class filled while clicking on button on jsp. In Database a new field is created but value are going null(Because model is not getting filled).
Please help me out on this.
Thanks in advance.
My Model class is:
package model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "conversion")
public class Conversion {
#Id
#GeneratedValue
private int conversionId;
#Column(name = "conversionFrom")
private String conversionFrom;
#Column(name = "conversionFromValue")
private int conversionFromValue;
#Column(name = "conversionTo")
private String conversionTo;
#Column(name = "conversionToValue")
private int conversionToValue;
#Column(name = "conversionDate")
private Date conversionDate;
public int getConversionId() {
return conversionId;
}
public void setConversionId(int conversionId) {
this.conversionId = conversionId;
}
public String getConversionFrom() {
return conversionFrom;
}
public void setConversionFrom(String conversionFrom) {
this.conversionFrom = conversionFrom;
}
public int getConversionFromValue() {
return conversionFromValue;
}
public void setConversionFromValue(int conversionFromValue) {
this.conversionFromValue = conversionFromValue;
}
public String getConversionTo() {
return conversionTo;
}
public void setConversionTo(String conversionTo) {
this.conversionTo = conversionTo;
}
public int getConversionToValue() {
return conversionToValue;
}
public void setConversionToValue(int conversionToValue) {
this.conversionToValue = conversionToValue;
}
public Date getConversionDate() {
return conversionDate;
}
public void setConversionDate(Date conversionDate) {
this.conversionDate = conversionDate;
}
}
My controller method is:
#RequestMapping("/saveConversion")
public ModelAndView saveConversion(HttpServletRequest request, HttpServletResponse response, Model model,
Conversion conversion) throws ServletException, IOException, ParseException {
System.out.println("In saveConversion");
Boolean boolean1 = serviceDao.setConversion(conversion);
if (boolean1) {
model.addAttribute("conversionSaved", "Conversion saved successfully!");
} else {
model.addAttribute("conversionSaved", "Conversion Not saved!");
}
return new ModelAndView("setConversion");
}
My Jsp is:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%#page import="model.Test"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Lexis Nexis Admin Set Conversion Page</title>
<!--css and js starts-->
<link type="text/css" rel="stylesheet" href="css/stylesheet.css" />
<script src="jQueryAssets/jquery_lib.js"></script>
<script>
function SaveConversion() {
var params = $("#Conversion_form").serialize();
alert(params);
$.ajax({
method : "POST",
url : "saveConversion",
data : {
'params' : params
},
success : function(result) {
alert(result);
$("#responseDiv").html(result);
messageDisplay();
return false;
}
})
}
function messageDisplay() {
alert("${conversionSaved}");
}
</script>
</head>
<body onload="clearData()">
<div class="mainpage">
<div class="add_admin_form">
<form id="Conversion_form" method="post" name="Conversion">
<div class="add_admin_form_label">
<spring:message code="CONVERSION_FROM" />
</div>
<div class="add_admin_form_field">
<select name="conversionFrom" id="conversionFrom" class="dropdown2">
<option value="USD"><spring:message code="USD" /></option>
<option value="INR"><spring:message code="INR" /></option>
</select> <input name="conversionFromValue" id="conversionFromValue"
type="text" class="textbox1" value="${conversion.conversionFromValue}" required />
</div>
<div class="add_admin_form_label">
<spring:message code="CONVERSION_TO" />
</div>
<div class="add_admin_form_field">
<select name="conversionTo" id="conversionTo" class="dropdown2">
<option value="INR"><spring:message code="INR" /></option>
<option value="USD"><spring:message code="USD" /></option>
</select> <input name="conversionToValue" id="conversionToValue" type="text"
class="textbox1" required />
</div>
<div class="add_admin_form_field">
<input type="button" value="" class="create"
onclick="SaveConversion()" />
</div>
</form>
</div>
</div>
<div class="clear"></div>
<div class="clear"></div>
</body>
</html>
My hibernate.cfg.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated file - Do not edit! -->
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- User / Password -->
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- Database Settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- for performance reasons changed to MyISAM from org.hibernate.dialect.MySQLInnoDBDialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/quotebuilder</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<!-- Database Scheme Auto Update -->
<property name="hbm2ddl.auto">update</property>
<!-- properties -->
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- <property name="connection.provider_class ">org.hibernate.connection.C3P0ConnectionProvider</property> -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.cache.use_structured_entries">false</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.timeout">100</property>
<!-- mapping files -->
<mapping class="model.User" />
<mapping class="model.Quotation" />
<mapping class="model.CustomerSize" />
<mapping class="model.CustomerSegment" />
<mapping class="model.Admin" />
<mapping class="model.Customer" />
<mapping class="model.CustomerTypeDiscount" />
<mapping class="model.EmployeeLevelDiscount" />
<mapping class="model.Product" />
<mapping class="model.ProductVolumeDiscount" />
<mapping class="model.TitleAuthorTag" />
<mapping class="model.TitleContentTag" />
<mapping class="model.Conversion" />
</session-factory>
</hibernate-configuration>
Dispatcher-servlet is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="login" class="controller.LoginController" />
<bean id="levelManagement" class="controller.LevelManagementController" />
<bean id="user" class="controller.UserController" />
<bean id="home" class="controller.HomeController" />
<bean id="report" class="controller.ReportController" />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="WEB-INF/properties/messages" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml is:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
pom.xml is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.logiquebrain</groupId>
<artifactId>QuoteBuilderAdmin</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>QuoteBuilderAdmin Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- Hibernate core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.2.Final</version>
</dependency>
<!-- optional -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.2.Final</version>
</dependency>
<!-- Add on 03-12-2015 -->
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version> </dependency> <dependency> <groupId>dom4j</groupId>
<artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency>
<groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId>
<version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version>
</dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId>
<version>3.1</version> </dependency> <dependency> <groupId>javax.transaction</groupId>
<artifactId>jta</artifactId> <version>1.1</version> </dependency> <dependency>
<groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId>
<version>2.5.1</version> </dependency> -->
<!-- Add on 03-12-2015 -->
<!-- Add on 04-12-2015 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<!-- Add on 04-12-2015 -->
<!-- Add on 07-12-2015 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<!-- Add on 07-12-2015 -->
</dependencies>
<build>
<finalName>QuoteBuilderAdmin</finalName>
<!-- Add on 07-12-2015 -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<!-- Add on 07-12-2015 -->
</build>
</project>
do the changes as mentioned by #alan and also make following changes to your js function:
function SaveConversion() {
var params = $("#Conversion_form").serialize();
alert(params);
$.ajax({
method : "POST",
url : "saveConversion",
data : params, //this line is changed
success : function(result) {
alert(result);
$("#responseDiv").html(result);
messageDisplay();
return false;
}
})
}
the js function as per the question was sending only one parameter "param" which had value of serialized form value. After making above change it should be working. I have verified the same using network tab of the browser. You can see it for yourself if needed.
If you want to Spring to automatically bind the submitted parameters to a Conversion instance then you need to use the #ModelAttribute annotation in your method signature.
#RequestMapping("/saveConversion")
public ModelAndView saveConversion(#ModelAttribute Conversion conversion, Model model) throws ServletException, IOException, ParseException {
System.out.println("In saveConversion");
Boolean boolean1 = serviceDao.setConversion(conversion);
if (boolean1) {
model.addAttribute("conversionSaved", "Conversion saved successfully!");
} else {
model.addAttribute("conversionSaved", "Conversion Not saved!");
}
return new ModelAndView("setConversion");
}
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args
An #ModelAttribute on a method argument indicates the argument should
be retrieved from the model. If not present in the model, the argument
should be instantiated first and then added to the model. Once present
in the model, the argument’s fields should be populated from all
request parameters that have matching names. This is known as data
binding in Spring MVC, a very useful mechanism that saves you from
having to parse each form field individually.
I have created a dynamic web module project usig STS and Spring MVC. The problem is I have add a string into a Model but it cannot be display on the JSP page using EL.
May I know what wrong with it?
Below is the details:
JSP Page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC</title>
</head>
<body>
Home
<br />
<c:out value="${message}" />
</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">
MVC Controller:
#Controller
public class HomeController {
public HomeController() {
super();
}
#RequestMapping(value="/home", method=RequestMethod.GET)
public ModelAndView showHomePage() {
// View Name - Model Name - Model Data
return new ModelAndView("home", "message", "Hello Spring MVC");
}
}
Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- http://www.jpalace.org/docs/tutorials/spring/mvc_10.html -->
<!-- Context Scan -->
<context:component-scan base-package="com.peter.controller"/>
<!-- Handler Mapping -->
<bean id="handlerMapping" class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- Handler Adapter - AnnotationMethodHandlerAdapter -->
<!-- Invoke Handler Method -->
<bean id="handlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean>
<!-- Pre/Post Handler Interceptor -->
<!--
Implement HandlerInterceptor
Declare HandlerInterceptor inside DefaultAnnotationHandlerMapping property or
globally inside <mvc:interceptors>
Need configure Filter object inside web.xml
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<bean class="pckg.MyInterceptor1"/>
<bean class="pckg.MyInterceptor2"/>
</list>
</property>
</bean>
<mvc:interceptors>
<bean class="pckg.MyInterceptor1"/>
<bean class="pckg.MyInterceptor2"/>
</mvc:interceptors>
-->
<!-- View Resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- Exception Resolver -->
<!-- Register Interceptor, Message Resource, Bean validation support, Message conversion and field formatting -->
<mvc:annotation-driven />
</beans>
I have JSTL.jar in my build path. There is warning message about the The tag handler class for "c:out" (org.apache.taglibs.standard.tag.rt.core.OutTag) was not found on the Java Build Path
Please help.
Thanks.
Download jstl-1.2.jar from the maven repo (http://repo1.maven.org/maven2/javax/servlet/jstl/1.2/).
Ensure that
the jar is available in WEB-INF\lib folder of your web application.
I would like to view your viewResolver configuration. Are you able to view the home page?? or there is 404 error?
If home.jsp is displaying properly then according to me the problem is in your jsp.
Look at the first line of the jsp where you have defined the page directive.
In that declaration remove the attribute isELIgnored="false" it is bydefault false everytime. So no need to define it explicitely.
I think if you remove that attribute. Your ${message} would display correctly.
Hope this helps you.
Cheers.