【问题标题】:Getting 404 page not found after adding ContextLoaderListener to spring mvc project with primefaces pages将ContextLoaderListener添加到带有primefaces页面的spring mvc项目后找不到404页面
【发布时间】:2022-01-11 18:24:24
【问题描述】:

我是 Spring 新手,正在尝试在 spring 和 primefaces 之间进行集成。添加ContextLoaderListenerRequestContextListener 后,当我在tomcat 上运行项目时,找不到404 页。 我的 web.xml 文件:

<?xml version = "1.0" encoding = "UTF-8"?>
   <web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
            xmlns = "http://java.sun.com/xml/ns/javaee" 
            xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            id = "WebApp_ID" version="2.5">
    
    <welcome-file-list>
      <welcome-file>login.xhtml</welcome-file>
   </welcome-file-list>
    
    <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
    
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.xhtml</url-pattern>
      <url-pattern>/faces/*</url-pattern>
   </servlet-mapping>
     
     <context-param>
        <param-name>contextConfigLocation</param-name>
         <param-value>
            /WEB-INF/spring/spring.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
    
</web-app>

这是我的 faces-config.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3">
 
    <application>
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
   </application>

     <navigation-rule>
        <display-name>/login.xhtml</display-name>
        <from-view-id>/login.xhtml</from-view-id>
    </navigation-rule>
    <navigation-rule>
        <display-name>/login.xhtml</display-name>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>viewRecords</from-outcome>
            <to-view-id>view.xhtml</to-view-id>
            <redirect/>
        </navigation-case>
    </navigation-rule>
</faces-config>

以下是我的控制器类:

    package com.testtask.nagarro.controller;

    import java.util.List;
    import java.util.Map;

    import javax.annotation.Resource;
    import javax.validation.Valid;

    import org.hibernate.annotations.Source;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    import com.testtask.nagarro.forms.LoginForm;
    import com.testtask.nagarro.forms.StatementForm;
    import com.testtask.nagarro.interfaces.StatementRepository;
    import com.testtask.nagarro.interfaces.UserService;
    import com.testtask.nagarro.models.Statement;

    @Controller
    public class NagarroController {
    
    @Autowired
    private UserService userService;
    
    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value= "/", method=RequestMethod.GET)
    public String loginPage() {
        
        return "login";
    }
    
    @RequestMapping(value="/login", method=RequestMethod.GET)
    public ModelAndView login() {
        
        ModelAndView model = new ModelAndView();
        model.setViewName("login");
        
        return model;
    }
    
    @RequestMapping(value="/viewRecords", method = RequestMethod.POST) 
    public String showHomePage(StatementForm form) {
        
        System.out.println("In view records controller");
        
        List<Statement> records = getUserService().getRecordsByAccountId(form.getAccountId());
        
        return "view";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processForm(@Valid LoginForm loginForm, BindingResult result, 
                                                    Map<String, LoginForm> model) {
        String username = "UserName";
        String password = "password";
        
        if(result.hasErrors()) {
            
            return "loginform";
        }
        
        loginForm = (LoginForm) model.get("loginForm");
        if(!loginForm.getUsername().equals(username) || !loginForm.getPassword().equals(password)) {
            return "loginForm";
        }
        
        model.put("loginForm", loginForm);
        return "home";
        
    }
    

}

我试图在网上找到解决方案,但我很困惑,因为大多数示例都使用 jsps。 请指出问题所在,我附上github链接。

【问题讨论】:

标签: java spring spring-mvc primefaces


【解决方案1】:

您的视图不在您在 InternalViewResolver bean 中定义的 webapp/WEB-INF 文件夹中。 它们位于 webapp 文件夹中。

【讨论】:

  • 应该在WEB-INF文件夹中吗?在我的调度程序-servlet 中,我添加了“/webapp/”作为 InternalResourceViewResolver 的前缀
  • 您没有使用 dispatcher-servlet.xml,您定义(有错字)您在 web.xml 文件中使用 spring.xml 顺便说一句,它应该是 /WEB-INF/spring。 xml 不是 /WEB-INF/spring/spring.xml
  • 另外,我发现你甚至在你的项目中注释掉了spring boot main方法,考虑到拼写错误和结构不好,我建议你,作为友好的建议,从小处着手,确保你可以运行你的意见和停止得到 404,之后,你可以堆积服务、模型、回购等,因为这个项目显然是同时听取 3 个不同人的建议的产物。从小处着手,然后逐步扩大规模。
  • 这是一个很好的例子,结构很好crunchify.com/…
  • 我更新了 repo。删除 dispatcher-servlet.xml 文件并将 jsf 文件作为 spring.xml 文件中的配置移动到视图文件夹。但是,仍然遇到同样的问题。我想说的是,当我在服务器上运行项目时遇到异常,异常是: Error created bean with name 'nagarroController': Injection of autowired dependencies failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:.... 如果这可能是问题,我可以编辑帖子并发布堆栈跟踪
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2021-07-20
  • 1970-01-01
  • 2020-03-08
  • 2017-09-21
相关资源
最近更新 更多