2017-03-15 189 views
-1

我的Spring项目出现问题。我刚开始使用Spring Boot,并试图将ez控制器重定向到另一个web。Spring Boot MVC映射

,当我开始我的应用程序并进入到浏览器上

本地主机:8080 /人

有问题映射IDK的为什么

enter image description here

这是我的结构 enter image description here

PersonController

package Controller; 

import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

import Model.Person; 

public class PersonController { 

     @RequestMapping("/person") 
     public String person(Model model) 
     { 
      Person p = new Person(); 
      p.setFirstName("John"); 
      p.setLastName("BonJovi"); 
      p.setAge(23); 
      model.addAttribute("person", p); 
      return "personview"; 
     } 
} 

Person类

package Model; 

public class Person { 
    String firstName; 
    String lastName; 
    int age; 

    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
    public int getAge() { 
     return age; 
    } 
    public void setAge(int age) { 
     this.age = age; 
    } 

} 

与 “主”

package demo; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Configuration 
@EnableAutoConfiguration 
@ComponentScan({"demo","controller"}) 


public class EducationProjectApplication { 

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

回答

1

添加@Controller您PersonController

此外的顶部 - 只是检查,你@ComponentScan({"demo","controller"})

“控制器” 不大写,但你的包被宣布为 “控制器”

+0

谢谢,它也帮助:) – artist

0

你必须注释你的PersonController类为@RestController

+0

好吧,它帮助,但如何显示细节的观点? – artist

+0

' <!DOCTYPE HTML!> <元的charset = “UTF-8”> 人WEB \t名字: \t姓: \t年龄: ' – artist

+0

对于这种情况,当您尝试返回HTML时,注释必须只有@Controller,并且您必须确保模型对应于jsp页面。 –

0

就像拉斐尔说的,你需要把注释放在PersonController类之上。 @RestController如果你想建立一个REST控制器,@Controller如果你想建立正常的网站。确保你已经配置好你的视图解析器,这样它会返回一个jsp文件。