GlassFish Server 状态 404 在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射

GlassFish Server status 404 No mapping found for HTTP request with URI in DispatcherServlet

我正在尝试将 JSF 2Spring 集成,我正在关注 this example 并制作一些修改以访问数据库并执行存储过程。

但是当我 运行 项目时,我从 GlassFish Server 获得了一个 Status 404 - Not found。在控制台日志中,我收到消息:

Warning:   No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcher'

这是我的结果代码:

文件夹结构

Initializer.java

public class Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(servletContext);
        servletContext.addListener(new ContextLoaderListener(ctx));
        servletContext.addListener(new RequestContextListener());
        Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);
   }
}

AppConfig.java

@Configuration
@ComponentScan("source")
class AppConfig {

    @Bean
    public Service service() {
        DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull", "root", "rootPass");
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        return new ServiceImpl(ds);
    }
}

ProcBean.java

@ManagedBean(name = "procBean", eager = true)
@RequestScoped
@Component
@RequestMapping("/")
public class ProcBean {

    private int input;
    private int output;
    @Autowired public Service procService;

    // Empty constructor, getters/setters

    public String callStoredProcedure() {
        this.output = procService.callStoredProcedure(input);
        return "output";
    }
}

ServiceImpl.java

public class ServiceImpl implements Service {

    private DataSource dataSource;
    private StoredProcedurePrueba prueba;

    public ServiceImpl(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    // get/set dataSource

    @Override
    public int callStoredProcedure(int input) {
        this.prueba = new StoredProcedurePrueba(dataSource);
        return this.prueba.execute(input);
    }

    private class StoredProcedurePrueba extends StoredProcedure {
        // Implementation tested separately and working correctly
    }
}

配置

面孔-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              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_2.xsd">

  <application>
    <el-resolver>
      org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
  </application>
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
         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-app_3_1.xsd">

  <display-name>JSF 2 + Spring 4 Integration Example</display-name>

    <servlet>
      <servlet-name>FacesServlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>FacesServlet</servlet-name>
      <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <session-config>
      <session-timeout>30</session-timeout>
    </session-config>
</web-app>

网页

<!-- input.xhtml -->
<h:body>
  <h3>JSF 2 + Spring 4 Integration Example</h3>
  <h:form id="studentForm">
    <h:outputLabel value="Enter Student id:" />
    <h:inputText value="#{procBean.input}" /> <br />
    <h:commandButton value="Submit" action="#{procBean.callStoredProcedure()}"/>
  </h:form>
</h:body>

<!-- output.xhtml -->
<h:body>
  <h3>JSF 2 + Spring 4 Integration Example</h3>
  <p>#{procBean.output}</p>
</h:body>

我正在尝试其他一些解决方案,但 none 对我有用。任何的想法?我错过了什么?

提前感谢您的回答。

不使用 Spring,但我认为您需要 web.xml 中的欢迎文件,例如

<welcome-file-list>
    <welcome-file>input.xhtml</welcome-file>
</welcome-file-list>