Marco MM
Marco MM

Reputation: 1

How can i use my Controller in spring mvc?

I have a problem with my Controller class: I create my project with maven and I add the dependencies for hibernate and Spring. When I start the app I can go in the first page from the servlet, but when I call any method I get 404. I try many solutions, but no one work, I use a web logic server, there is some particular configuration for this server? In the project this is my actual situation:

AppController.java

@Controller
@RequestMapping("/")
public class AppController {


        @RequestMapping(value = "/two", method = RequestMethod.GET)
        public String two() {
            System.out.println("work?");
            return "newFile";
        }

}

i try also with ModelAndView

registration.jsp

<!DOCTYPE HTML>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Title</title>
      <style type="text/css">
         form {
         display: inline-block;
         }
      </style>
   </head>
   <body>
      <form action="/two" method="GET">
         <p class="message">//Not registered? 
            <input type="submit" value="Create an account">
         </p>
      </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>
   <servlet>
      <servlet-name>LDSServlet</servlet-name>
      <servlet-class>com.reply.servlet.LDSServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>LDSServlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
   <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>
</web-app>

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-3.0.xsd  
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
         <value>/</value>
      </property>
      <property name="suffix">
         <value>.jsp</value>
      </property>
   </bean>
</beans>

AppConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.reply.utility")
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        mesageSource.setBasename("messages");
        return messageSource;
    }
}

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.reply</groupId>
  <artifactId>LDSSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>LDSSpring</name>
  <url>http://maven.apache.org</url>



  <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>


  <properties>
        <springframework.version>5.1.0.RELEASE</springframework.version>
        <hibernate.version>5.3.1.Final</hibernate.version>
        <oracle.connector.version>18.1.0.1</oracle.connector.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate.version}</version>
        </dependency>









         <!-- https://mvnrepository.com/artifact/org.jboss/jandex -->
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jandex</artifactId>
            <version>2.0.5.Final</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>


        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>



    </dependencies>
</project>

In the JSP and in the method I try to write the value for call method with / and without, but the result is the same. Can someone help me?

Upvotes: 0

Views: 331

Answers (3)

Marco MM
Marco MM

Reputation: 1

I resolve my problem, the process "oracle xe tns listener" use my port 8080, i suppose the server block the call on controller. I found this solution because i try to start my application on tomcat and i had the port busy, so I termitate the process and my application go on tomcat and web logic server :)

Upvotes: 0

greengreyblue
greengreyblue

Reputation: 389

If I understand the problem correctly, you having new controller component and request mapping which should be invoked on form submission.

I see the below issues in xml configuration

you are not using spring DispatcherServlet in web.xml. This class is responsible for processing and handles the request and forwards to appropriate controller

You are defining the mapping in web.xml with class com.reply.servlet.LDSServlet. how would the LDSServlet class look like?

Basically DispatcherServlet, will process the configuration (Controller classes and request mappings) and uses this for picking the appropriate controller while request arrives.

EDIT:

you should be including the context-path in the form action.

Upvotes: 1

chandrakant
chandrakant

Reputation: 370

try this way, it is work fine with me.

@Controller
public class HelloController {

    @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
    public ModelAndView welcomePage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is welcome page!");
        model.setViewName("hello");
        return model;

    }

Upvotes: 0

Related Questions