admin管理员组

文章数量:1125558

This was my SpringBootWebApplication class

package com.learning.SpringBootWeb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWeb1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWeb1Application.class, args);
    }

}

This is my HomeController class

package com.learning.SpringBootWeb;

import org.springframework.stereotype.Controller;
import 'org.springframework.web.bind.annotation.GetMapping';

@Controller
public class HomeController {
    @GetMapping("/")
    public String home() {
        return "index.jsp";
    }
}

I had both the classes in the same package but still controller not getting recognized and i am getting the default error page.

What changes should i do to make work I do have proper dependencies added in my pom.xml file

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- .apache.tomcat/tomcat-jasper -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>10.1.34</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

This was my SpringBootWebApplication class

package com.learning.SpringBootWeb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWeb1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWeb1Application.class, args);
    }

}

This is my HomeController class

package com.learning.SpringBootWeb;

import org.springframework.stereotype.Controller;
import 'org.springframework.web.bind.annotation.GetMapping';

@Controller
public class HomeController {
    @GetMapping("/")
    public String home() {
        return "index.jsp";
    }
}

I had both the classes in the same package but still controller not getting recognized and i am getting the default error page.

What changes should i do to make work I do have proper dependencies added in my pom.xml file

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>10.1.34</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Share Improve this question edited Jan 9 at 7:12 asgarov1 4,0502 gold badges10 silver badges23 bronze badges asked Jan 9 at 7:06 Kireeti NunnaKireeti Nunna 31 bronze badge New contributor Kireeti Nunna is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • please show your complete pom.xml – asgarov1 Commented Jan 9 at 7:13
  • did you package your application as WAR? See docs.spring.io/spring-boot/reference/web/… – Brian Clozel Commented 2 days ago
Add a comment  | 

4 Answers 4

Reset to default 0

Make sure to maintain this package structure:

src/
  main/
    java/
      com/
        learning/
          SpringBootWeb/
            SpringBootWeb1Application.java
            HomeController.java
    webapp/
      WEB-INF/
        index.jsp

instead of @Controller annotation use @RestController.if you want to use @Controller then add @ResponseBody below @GetMapping("/")

Thank you guys, I got it solved. Instead of using the return type as string i have used ModelAndView and it got fixed.

package com.learning.SpringBootWeb;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

    @RequestMapping("/")
    public ModelAndView home()
    {
        return new ModelAndView( "index");
    }
    
    @RequestMapping("/add")
    public ModelAndView add(@RequestParam("input1") int num1, @RequestParam("input2") int num2, Model model) {
        
        int result = num1+num2;
        
        model.addAttribute("result", result);
        
        return new ModelAndView("result");
    }

}

Try using @RestController instead of @Controller.

本文标签: Controller is not working in my spring boot applicationStack Overflow