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
|
4 Answers
Reset to default 0Make 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
版权声明:本文标题:@Controller is not working in my spring boot application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736666783a1946706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pom.xml
– asgarov1 Commented Jan 9 at 7:13