Space
Space

Reputation: 213

Spring MVC project only redirected to tomcat page instead using intellij. How to solve?

I am using spring framework 5.0.4 and tomcat server 9.0.2.

And its a maven project.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     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">

<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>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.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>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>    
</web-app>

dispatcher-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"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/cache/spring-tx.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">

<mvc:annotation-driven/>

<context:component-scan base-package="com.project.springbase"/>

<!-- This is the location under /webapp/resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- Can also use the default maven resources folder with subfolder name-->
<!--<mvc:resources mapping="/resources/**" location="classpath:/foo/" />-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->

</beans>

applicationContext.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:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/tx/spring-context-4.0.xsd">

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/datasource-cfg.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}" />
 </beans>

Here is my intellij server console when deploying project: enter image description here

And here is the browser output: enter image description here

Everytime i deploy or run the project this page comes instead of my index.

And here is my home controller along with the project strcture: enter image description here

With all my configurations, whenever i run the project, it shows the tomcat page on localhost:8080 instead of my project index.jsp.

What could be the possible reason?

Upvotes: 0

Views: 629

Answers (2)

Itumeleng
Itumeleng

Reputation: 11

Try this tutorial, it helped me. also change change your port number

Step 1 create Spring project on IntelliJ

Additional libraries

  • Spring MVC and
  • Java EE: Web Application

Step 2 in web.xml - change

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>

- to

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>

Step 3 Edit your dispatcher-servlet.xml

 <context:component-scan base-package="com.springdemo" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

Step 4 Create your Controller.java

Step 5 Add Tomcat Server

  • Run > Edit Configuration > Click "+" > Tomcat Server > Local

Still inside Run/Debug Configurations dialog box

  • Warning: No artifacts marked for deployment > Click Fix
  • on Application context: "/" remove name_war_exploded
  • under Server I change my URL tohttp://localhost:8082/
  • Also on HTTP Port: 8082

Step 6

  • File > Project Structure > Problems (2) >
  • Click Fix > add all all missing dependancies

Step 7

  • delete index.jsp in directory web
  • create a new index.jsp in directory web > WEB-INF > views

Upvotes: 1

streetturtle
streetturtle

Reputation: 5850

In Run Configuration you have following section:

enter image description here

So you either need to uncheck the After Launch action or change URL to http://localhost:8080/index.jsp or whatever else.

Upvotes: 0

Related Questions