gpt4 book ai didi

jsf - JSF Web 应用程序中不断出现“目标无法访问”、标识符解析为 null 的情况

转载 作者:行者123 更新时间:2023-12-02 03:28:29 25 4
gpt4 key购买 nike

我这里有一个简单的问题我正在制作一个简单的 jsf Web 应用程序,可以上传文件但我不断得到

Target Unreachable, identifier 'upload' resolved to null

这是我的上传类

package SimpleLogin;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;

@ManagedBean
@RequestScoped
public class upload {

private UploadedFile uploadedFile;

public void submit() throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();

FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
}

public UploadedFile getUploadedFile() {
return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}

}

和我的索引页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk"
>
<h:head>
<title>Facelet Title</title>
<h:outputStylesheet name="css/jsfcrud.css"/>
</h:head>
<h:body>
<ui:composition template="./template.xhtml">

<ui:define name="body">
<h:form>
<h:commandLink action="/contacts/List" value="Show All Contacts Items"/>
</h:form>

<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{upload.uploadedFile}" />
<h:commandButton value="submit" action="#{upload.submit}" />
<h:messages />
</h:form>

</ui:define>
</ui:composition>

哦,是的,这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>

抱歉让您久等了这是我的脸 config.xml

<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns /javaee/web-facesconfig_2_0.xsd">

<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>SimpleLogin.simpleLogin</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>SimpleLogin.Upload</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope></managed-bean>

<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-action>
#{simpleLogin.CheckValidUser}
</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>/resultforfail.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>
#{simpleLogin.CheckValidUser}
</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<resource-bundle>
<base-name>/Bundle</base-name>
<var>bundle</var>
</resource-bundle>
</application>

和我的模板

 <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title">Default Title</ui:insert></title>
<h:outputStylesheet name="css/jsfcrud.css"/>
<h:outputStylesheet name="css/cssLayout.css"/>
<h:outputStylesheet name="css/default.css"/>
</h:head>

<h:body>
<div id="top">
<ui:insert name="top">Ini Header</ui:insert>
</div>

<h1>
<ui:insert name="title"></ui:insert>
</h1>
<p>
<ui:insert name="body"></ui:insert>
</p>

<div id="bottom">
<ui:insert name="bottom">Ini Footer</ui:insert>
</div>

</h:body>

问题是什么?我没有明确说明上传类别吗?任何帮助都是好的:)

最佳答案

您已覆盖 @ManagedBean通过<managed-bean>关于 faces-config.xml 的声明,但是<managed-bean>声明具有不同的托管 bean 名称,即 bean而不是upload .

你基本上有 3 个选择:

  1. 使用#{bean}而不是#{upload}在你看来。

  2. 重命名 <managed-bean-name>faces-config.xml来自beanupload这样您就可以使用#{upload}在你看来。

  3. 摆脱<managed-bean>总共这样@ManagedBean将会被使用。托管 bean 名称默认为第一个字符小写的 bean 类名称,因此 #{upload}应该可以。

剩下的,你和Java naming conventions不一致。请也为此努力。包名称应全部小写,类名称应采用 CamelCase 并以大写字母开头。

关于jsf - JSF Web 应用程序中不断出现“目标无法访问”、标识符解析为 null 的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8693692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com