#Spring MVC
205 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @whole lichen! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
@floral stratus hey bro can u help me please
Caused by: java.lang.NoClassDefFoundError: antlr/collections/List
That's your problem.
๐ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
hey, i think your implementation class is having mistake
try to rewrite your impl methods
๐ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
can u please ellaborate ? @exotic pelican
@floral stratus hey broo can u look 0ver it and check the error bcz i am really stuck into it .
But i remove the class and found some other error that looks like this https://sourceb.in/fNII4Iux9
@floral stratus
What's the pom.xml? How do you run the application?
How do u run the application? Means
Why are you using version 5.3.20 for spring-orm as opposed to 5.3.18?
How you are running the application
mvn spring-boot:run?
Do you deploy a WAR file?
No it's not a spring boot app it's spring MVC project
No
So how are you doing it?
I am deploy it to the tomcat server
and how?
Does that mean it isn't a WAR file?
ok that's what I wanted to know
What about that?
Is there a reason for this?
Like I think this is the only way to start a project in spring MVC or in Servlets?
One of many ๐
So is this the actual reason why the error is coming?
I want to know about this
this could be the reason for the error
Unlikely
But now I have changed it
what did you change to what?
No idea, it seems that you removed the error.
From the version 5.3.20 to 5.3.18 of spring orm
.
here is the error
in think it is now changed
this is now how the code looks like https://sourceb.in/8OJ660Ck38
Like how bcz i have provided it everything that it wants ...
there was only one single answer Hibernate Bean Configuration File was not loaded in Web.xml file. and i think its correct in my project
I don't know if beans defined in XML are any different, but you can try adding @Qualifier("hibernateTemplate")
Nahh that doesn't work
Do you have a web.xml?
Yehh
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<absolute-ordering></absolute-ordering>
<servlet>
<servlet-name>LoginPage</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcherservlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LoginPage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>```
Do you have anything else in the WEB-INF?
this is how it looks likes
What's the config.xml?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config.xml</param-value>
</context-param>
try adding that to the web.xml
https://sourceb.in/mmrrpBLKq9 i think the issue is still same
that's a different error
Ohh yehh
Do you have hibernate-core in your pom.xml?
Yehh
Can you try changing the version of hibernate-core to 5.5.4?
actually 5.6.7.Final is probably better
and 6.2.3.Final for Hibernate validator
see https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/2.6.6/spring-boot-dependencies-2.6.6.pom for versions that should match
Can you show it?
Did you reload the Maven project and restart tomcat after the change?
You could try putting hibernate and other dependencies into WEB-INF/lib
if you don't know how to do that, you could also get Maven to do it
I'm currently writing the configuration for it
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<configuration>
</configuration>
<executions>
<execution>
<id>copy-libs</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/webapp/WEB-INF/lib</outputDirectory>
<includeScope>compile</includeScope>
<includeGroupIds>org.hibernate</includeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
You could try adding this to the pom.xml
this should attempt to copy the hibernate stuff to WEB-INF/lib
After doing that, it should look like this
Essentially that configures the dependency plugin (which manages dependencies) to automatically copy certain dependencies in the generate-resources phase. It is then configured to only do this for dependencies in org.hibernate with the scope compile (the default scope) and to copy it to WEB-INF/lib
So when i shift to spring boot from this then should i have to do it everytime
THis is normally not necessary with Spring Boot
Does it fix the error?
if you change <outputDirectory>${project.basedir}/src/main/webapp/WEB-INF/lib</outputDirectory> to <outputDirectory>${project.basedir}/src/main/resources/WEB-INF/lib</outputDirectory>, it doesn't change anything, right?
another thing yu could try is to right-click your project and select build path>configure build path. There, check Maven Dependencies under Order and Export
https://sourceb.in/evukY5GBoo The error i got after doing this !
๐ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
Hey did u find why the error is coming @floral stratus
tbh idk
Actually what's your LoginPage?
Maybe the issue is with your config.xml
and maybe you could try that
In the build path, go to Add Library
select Web App Libraries
make sure the right project is selected
and click Finish
then it should look somewhat like that
Actually when did not connect the spring with orm then it was working fine but when I connected it with orm and try to get hibernate Template then it starts giving me errors
Do you really need HibernateTemplate?
I think HibernateTemplate doesn't even exist in newer Spring versions (you are using an old version of Spring)
So then how to work with orm in spring?
I think you are right bcz the video which I am following rn is 3 yrs old and in the comments people are also getting the same type of error as me
I think its's possible to directly use SessionFactory instead of HibernateTemplate
videos are a problem in general
maybe everything related to HibernateTemplate?
But I think you have problems with SessionFactory, right?
Yehh
If you show me your current web.xml, config.xml and controller (as text, not with images or links to other sites), I may be able to reproduce it by myself
What's com.controllers.Database.DaoEntity?
its database entity where i have made dao inteface and its implemented class and a Dao Entity
which i have creted using hibernate
ok so for me it works but I made some changes
since you now don't have the dispatcherservlet.xml in your web.xml, I added this to the config.xml:
<bean id = "viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/lib/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
also if yoou expand Web App Libraries>hibernate-core and select dialect, is there a MySQL55Dialect or MySQL8Dialect class there?
yehh it is here
Which one is there?
The thing is that I'm using a newer version of all of these things so a bit of it is different
but it does work for me
there are 2 of them 1st is MySQL8Dialect.class and the second one is MySQL55Dialect.class as well
ok I think MySQL8Dialect is better for you
so in your config.xml, also change <prop key="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</prop> to <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
and you also have this in the config.xml:
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
comment this out
i.e. you can replace it with
<!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>-->
``` and it will no longer be active
and after you did all that, tell me what happens
so i have to change the dialect from 55 to 8 and comment this out right ?
yes
and don't forget about that
because you have the config.xml instead of the dispatcherservlet.xml now so you still need these parts
which lines?
<property name="prefix" value="/WEB-INF/lib/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>```
but i already have it in dispatcher-servlet.xml
Well you no longer use that in your web.xml, right?
ohh yeh i have passed the name of the config.xml file in the web.xml
so now i got another error bcz i have also comment out the hibernate template bean
@floral stratus
How is that another error?
Also why do you use that sourcebin site instead of just posting these things in Discord using a codeblock?
You still have this
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Try commenting this out as well
Also can you show the code you are using HibernateTemplate with? You likely need to change it
For me, it's possible to use Hibernate somewhat like that (using SessionFactory and not HibernateTemplate):
@Controller
public class Login {
private final SessionFactory sessionFactory;
public Login(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@RequestMapping("/hi")
public String hi() {
System.out.println("Hii");
try(EntityManager em = sessionFactory.createEntityManager()){
DaoEntity entity = new DaoEntity();
entity.setId(123L);
entity.setName("test");
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
}
return "hi";
}
@RequestMapping("/ho")
public String ho() {
System.out.println("Ho");
try(EntityManager em = sessionFactory.createEntityManager()){
System.out.println(em.find(DaoEntity.class, 123L).getName());
}
return "hi";
}
}
btw please follow naming conventions in your code if possible: #1241797526374580325 message
Naming conventions ensure that projects have similar names and allow to distinguish e.g. packages, classes and methods/variables. For that, it is important developers don't mix naming conventions.
Most Java code follows these naming conventions:
- Type names (e.g. classes or interfaces) should be PascalCase meaning the first letter of every word is written in uppercase while all other letters are lowercase.
- Constants are ALL_UPPERCASE_SEPARATED_BY_UNDERSCORES.
- Other variables (both fields and local variables) as well as methods are camelCase. This means the identifiers start with a lowercase letter, all subsequent words start with an uppercase letter and all other letters are lowercase.
- Package names are all_lowercase whereas dots are used to create a hierarchy of packages. All packages of an application are typically grouped together in some top level package. This typically uses reverse domain name notation. For example, an application called
MyApplicationmaintained by a company/individual owning the domainexample.comwould put all its classes in a package namedcom.example.myapplicationor subpackages.
package com.mydomain.myproject.somesubpackage;
public class ClassNamesArePascalCase{
private static final String CONSTANTS_ARE_UPPER_CASE = "Hello World";
private int variablesAreCamelCase = 13;
public void methodNamesAreCamelCase(){
int localVariablesAreAlsoCamelCase = 37;
}
}
There are other naming conventions as well (for example prepending getters with get and setters with set, e.g. getMyVariable/setMyVariable.
Aside from that, projects may have specific naming conventions.
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class DaoEntityImp implements Dao {
@Autowired
private HibernateTemplate hibernateTemplate;
@Transactional
@Override
public int saveUser(DaoEntity daoEntity) {
return (int) this.hibernateTemplate.save(daoEntity);
}
@Override
public DaoEntity getUser(int userId) {
DaoEntity daoEntity = this.hibernateTemplate.get(DaoEntity.class, userId);
return daoEntity;
}
@Override
public int deleteUser(int userId) {
// TODO Auto-generated method stub
return 0;
}
}
here is the class where i am using the Hibernate Template
try it like this:
package com.controllers.Database;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class DaoEntityImp implements Dao {
private final EntityManager entityManager;//I CHANGED THIS
public DaoEntityImp(SessionFactory sessionFactory) {//I ADDED THIS CONSTRUCTOR
this.entityManager = sessionFactory.createEntityManager();
}
@Override
public int saveUser(DaoEntity daoEntity) {
this.entityManager.getTransaction().begin();//these things are instead of @Transaction, there is a better way but this way should definitely work
try{
this.entityManager.persist(daoEntity);
this.entityManager.getTransaction().commit();
return 1;//no idea what you actually want to return here
} catch(RuntimeException e) {
this.entityManager.getTransaction().rollback();
throw e;
}
}
@Override
public DaoEntity getUser(int userId) {
DaoEntity daoEntity = this.entityManager.get(DaoEntity.class, userId);
return daoEntity;
}
@Override
public int deleteUser(int userId) {
// TODO Auto-generated method stub
return 0;
}
}
Alternatively you could replace the constructor with
public Login(EntityManagerFactory emf) {
this.entityManager = emf.createEntityManager();
}
Alternatively if you want to use @Transactional, you could try this:
@Repository
public class DaoEntityImp implements Dao {
private final SessionFactory sesionFactory;//I CHANGED THIS
public DaoEntityImp(SessionFactory sessionFactory) {//I ADDED THIS CONSTRUCTOR
this.sessionFactory = sessionFactory;
}
@Transactional
@Override
public int saveUser(DaoEntity daoEntity) {
this.sessionFactory.getCurrentSession().save(daoEntity);
return 1;//idk what you want to return here
}
@Override
public DaoEntity getUser(int userId) {
DaoEntity daoEntity = this.sessionFactory.getCurrentSession().get(DaoEntity.class, userId);
return daoEntity;
}
@Override
public int deleteUser(int userId) {
// TODO Auto-generated method stub
return 0;
}
}
Ok I will try it later after reaching my home
But can you tell me please bro can I skip the spring MVC project bcz like I have completed the basics annotation and basic understanding of spring MVC so can I go for spring boot directly or stayed with it and make projects ?
yeah sure
Does it effect anything in my future ?
What specifically?
In most cases, Spring is used with Spring Boot
and tbh I've never used Spring without Spring Boot but as you have probably seen, I understand how it works enough so I can probably fix most problems (if I encounter a problem, I am more likely to properly understand it than when someone else encounters it because if it's on my machine, I can easily reproduce it and it's also something important for me so I'm willing to spend time on it)
So essentially I don't think you need to know how to use everything with Spring without Spring Boot but it's good to understand how these things work
So directly jumping into spring boot now will not be the bad decision for me .. And the main reason I am thinking to directly switch to spring boot is bcz i have completed spring core , spring MVC basics , spring with orm so I think this is enough to jump to spring boot right?
Does it important to learn spring JDBC Module as well ?
You can also use Spring JDBC with Spring Boot