#Spring MVC

205 messages ยท Page 1 of 1 (latest)

whole lichen
glad spearBOT
#

โŒ› This post has been reserved for your question.

Hey @whole lichen! Please use /close or the Close Post button 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.

whole lichen
#

@floral stratus hey bro can u help me please

crisp rock
#

Caused by: java.lang.NoClassDefFoundError: antlr/collections/List

#

That's your problem.

glad spearBOT
#

๐Ÿ’ค 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.

exotic pelican
#

hey, i think your implementation class is having mistake

#

try to rewrite your impl methods

glad spearBOT
#

๐Ÿ’ค 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.

whole lichen
#

@floral stratus hey broo can u look 0ver it and check the error bcz i am really stuck into it .

whole lichen
#

@floral stratus

floral stratus
#

What's the pom.xml? How do you run the application?

whole lichen
#

On server

#

and here is the pom.xml '

#

@floral stratus

floral stratus
#

and you don't need to ping me

whole lichen
#

How do u run the application? Means

floral stratus
#

Why are you using version 5.3.20 for spring-orm as opposed to 5.3.18?

floral stratus
#

mvn spring-boot:run?

#

Do you deploy a WAR file?

whole lichen
#

No it's not a spring boot app it's spring MVC project

whole lichen
floral stratus
whole lichen
#

I am deploy it to the tomcat server

floral stratus
#

and how?

floral stratus
whole lichen
#

Basically I am adding the project to the tomcat server like this

floral stratus
#

ok that's what I wanted to know

floral stratus
#

Is there a reason for this?

whole lichen
#

Like I think this is the only way to start a project in spring MVC or in Servlets?

crisp rock
#

One of many ๐Ÿ˜„

whole lichen
#

So is this the actual reason why the error is coming?

floral stratus
#

this could be the reason for the error

floral stratus
whole lichen
#

But now I have changed it

floral stratus
crisp rock
#

No idea, it seems that you removed the error.

whole lichen
#

From the version 5.3.20 to 5.3.18 of spring orm

whole lichen
floral stratus
#

Did that change anything?

#

Are you getting the same error again?

whole lichen
#

here is the error

#

in think it is now changed

floral stratus
#

ok that seems somewhat better

#

looks like the HibernateTemplate has an issue

whole lichen
#

Like how bcz i have provided it everything that it wants ...

floral stratus
whole lichen
#

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

crisp rock
#

I don't know if beans defined in XML are any different, but you can try adding @Qualifier("hibernateTemplate")

whole lichen
#

Nahh that doesn't work

floral stratus
#

Do you have a web.xml?

whole lichen
#

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>```
floral stratus
#

Do you have anything else in the WEB-INF?

whole lichen
#

this is how it looks likes

floral stratus
#

What's the config.xml?

whole lichen
#

The spring configuration

floral stratus
#
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config.xml</param-value>
    </context-param>
#

try adding that to the web.xml

whole lichen
floral stratus
#

that's a different error

whole lichen
#

Ohh yehh

floral stratus
#

Do you have hibernate-core in your pom.xml?

whole lichen
#

Yehh

floral stratus
#

Can you try changing the version of hibernate-core to 5.5.4?

whole lichen
#

i found

floral stratus
#

and 6.2.3.Final for Hibernate validator

whole lichen
#

the error

floral stratus
#

in your pom.xml

whole lichen
#

i did it

#

but still facing the error

floral stratus
#

Can you show it?

whole lichen
#

yeh sure

floral stratus
#

Did you reload the Maven project and restart tomcat after the change?

whole lichen
#

yeh

#

alt +f5 in eclipse

floral stratus
#

Do you have a build directory in the project?

#

or target

#

What's in WEB-INF/lib?

whole lichen
#

This is inside of the lib folder

#

this is what target folder have inside

floral stratus
#

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

whole lichen
#

how to do it

#

?

floral stratus
#

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

floral stratus
# floral stratus ```xml <plugin> <groupId>org.apache.maven.plugins</g...

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

whole lichen
#

So when i shift to spring boot from this then should i have to do it everytime

floral stratus
#

Does it fix the error?

whole lichen
#

No ๐Ÿ™

floral stratus
#

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

whole lichen
glad spearBOT
#

๐Ÿ’ค 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.

whole lichen
#

Hey did u find why the error is coming @floral stratus

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

whole lichen
#

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

floral stratus
#

Do you really need HibernateTemplate?

#

I think HibernateTemplate doesn't even exist in newer Spring versions (you are using an old version of Spring)

whole lichen
#

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

floral stratus
#

I think its's possible to directly use SessionFactory instead of HibernateTemplate

floral stratus
whole lichen
#

Hmm yehh u are right

#

So now what should I have to remove from my config.xml

floral stratus
#

maybe everything related to HibernateTemplate?

#

But I think you have problems with SessionFactory, right?

whole lichen
#

Yehh

floral stratus
#

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

whole lichen
#

Alright give me 3 min

floral stratus
#

What's com.controllers.Database.DaoEntity?

whole lichen
#

its database entity where i have made dao inteface and its implemented class and a Dao Entity

#

which i have creted using hibernate

floral stratus
#

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?

whole lichen
#

yehh it is here

floral stratus
#

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

whole lichen
#

there are 2 of them 1st is MySQL8Dialect.class and the second one is MySQL55Dialect.class as well

floral stratus
#

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

whole lichen
#

so i have to change the dialect from 55 to 8 and comment this out right ?

floral stratus
#

yes

floral stratus
#

because you have the config.xml instead of the dispatcherservlet.xml now so you still need these parts

whole lichen
#

so u mean i have to add these lines to the config.xml aswell

#

?

#

@floral stratus

floral stratus
whole lichen
#
      
          <property name="prefix" value="/WEB-INF/lib/view/"></property>
          <property name="suffix" value=".jsp"></property>
      
      </bean>```
floral stratus
#

yes

#

if you still want to use JSP, you probably need it

whole lichen
#

but i already have it in dispatcher-servlet.xml

floral stratus
#

Well you no longer use that in your web.xml, right?

whole lichen
#

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

whole lichen
#

@floral stratus

floral stratus
#

Also why do you use that sourcebin site instead of just posting these things in Discord using a codeblock?

whole lichen
#

The config file now

floral stratus
#

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";
    }
}
raven siloBOT
#

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 MyApplication maintained by a company/individual owning the domain example.com would put all its classes in a package named com.example.myapplication or 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.

whole lichen
#

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

floral stratus
#

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;
    }
}
whole lichen
#

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 ?

floral stratus
#

yeah sure

whole lichen
#

Does it effect anything in my future ?

floral stratus
#

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

whole lichen
#

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 ?

floral stratus
#

You can also use Spring JDBC with Spring Boot