PageNotFound - 在DispatcherServlet中找不到带有URI []的HTTP请求的映射,名称为“dispatcher”

时间:2015-06-05 06:20:48

标签: java spring spring-mvc servlets

在我的Spring MVC应用程序中,我的所有RequestMapping都被正确映射,除了一个。我无法弄清楚为什么会出现PageNotFound错误。对于方法addSatisfaction,我收到PageNotFound - No mapping found for HTTP request with URI [/CCHPWeb/heart2heart/feedback/102/resolution/satisfaction] in DispatcherServlet with name 'dispatcher'

我的Controller

@Controller
@RequestMapping("/heart2heart/feedback")
public class H2HFeedbackController {

    private static final Logger logger = LoggerFactory.getLogger(H2HFeedbackController.class);
    private final ActivitiService activitiService;
    private final Heart2HeartService heart2heartService;

    @Autowired
    public H2HFeedbackController(ActivitiService activitiService, Heart2HeartService heart2heartService) {
    super();
    this.activitiService = activitiService;
    this.heart2heartService = heart2heartService;
    }

    @RequestMapping(value = "/${feedbackId}/resolution/satisfaction", method = RequestMethod.GET)
    public String getSatisfaction(@PathVariable int feedbackId, Model model) {
    Feedback feedback = new Feedback();
    feedback.setId(feedbackId);
    try {
        feedback = heart2heartService.getFeedbackById(feedback);
        if (feedback.getId() == 0) {
        model.addAttribute("error", "Feedback does not exist");
        model.addAttribute("status", "404 - Not Found");
        return "error";
        }
        model.addAttribute("feedback", feedback);
    } catch (Exception e) {
        logger.error("Exception :: ", e);
    }
    return "heart2heart/closeFeedback";
    }

    @RequestMapping(value = "/{feedbackId}", method = RequestMethod.GET)
    public String viewFeedback(@PathVariable int feedbackId, Model model) {
    Feedback feedback = new Feedback();
    feedback.setId(feedbackId);
    try {
        feedback = heart2heartService.getFeedbackById(feedback);
        if (feedback.getId() == 0) {
        model.addAttribute("error", "Feedback does not exist");
        model.addAttribute("status", "404 - Not Found");
        return "error";
        }
        model.addAttribute("feedback", feedback);
    } catch (Exception e) {
        logger.error("Exception :: ", e);
    }
    return "heart2heart/feedbackView";
    }
}

我的WebApplicationInitializer是:

public class SiteMain implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(MvcConfig.class);
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

2 个答案:

答案 0 :(得分:1)

你在这里不需要$:

/${feedbackId}/resolution/satisfaction

将其更改为

/{feedbackId}/resolution/satisfaction

$代表'字符串结束(或行)'在正则表达式。我不认为你在路径映射中确实需要它,因为它对整数feedbackId没有意义。

答案 1 :(得分:0)

@khateeb,5月web.xml问题请找到以下web.xml内容。<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>