【问题标题】:Unresolved compilation problems: The type java.lang.String cannot be resolved. Implicit constructor Object() is undefined未解决的编译问题: java.lang.String 类型无法解析。隐式构造函数 Object() 未定义
【发布时间】:2015-11-30 23:05:33
【问题描述】:

我在 Eclipse Java EE IDE(Luna Release 2 (4.4.2) 和 Java jre1.8.0_66)中创建了一个 Java 类。

package com.luv2code.jsf.hello;
 import javax.faces.bean.ManagedBean;



 @ManagedBean
 public class Student {


     private String sfirstName;
     private String slastName;

     // create a no-arg constructor
     public Student() {

     }

     public String getFirstName() {
         return sfirstName;
     }

     public void setFirstName(String sfirstName) {
         this.sfirstName = sfirstName;
     }

     public String getLastName() {
         return slastName;
     }

     public void setLastName(String slastName) {
         this.slastName = slastName;
     }

     // define public getter and setter methods to expose properties of the managed Bean

 }

我得到的错误是:

HTTP Status 500- Unresolved compilation problems
type Exception report
message unresolved compilation problem
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Unresolved compilation problems: 
 The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
 String cannot be resolved to a type
 String cannot be resolved to a type
 Implicit super constructor Object() is undefined. Must explicitly invoke another constructor
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type

 javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.Error: Unresolved compilation problems: 
 The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
 String cannot be resolved to a type
 String cannot be resolved to a type
 Implicit super constructor Object() is undefined. Must explicitly invoke another constructor
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type
 String cannot be resolved to a type

 com.luv2code.jsf.hello.Student.<init>(Student.java:1)r





com.luv2code.jsf.hello.Student.<init>(Student.java:1)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 java.lang.reflect.Constructor.newInstance(Unknown Source)
 java.lang.Class.newInstance(Unknown Source)
 com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
 com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
 com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
 com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
 com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
 com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
 com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
 com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
 org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:80)
 org.apache.el.parser.AstValue.getValue(AstValue.java:137)
 org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
 com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
 javax.faces.component.UIOutput.getValue(UIOutput.java:174)
 javax.faces.component.UIInput.getValue(UIInput.java:291)
 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
 javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
 javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
 javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
 com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
 com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

如果我将鼠标悬停在字符串上,我会看到

字符串无法解析为类型

如果我将鼠标悬停在 Student(无参数构造函数)上,我会看到:

隐式构造函数 Object() 未定义。必须显式调用另一个构造函数。

我不确定为什么它不允许我在 m Student 类中使用私有字符串。 谁能解释一下或者给我一些建议?

【问题讨论】:

    标签: java eclipse compiler-errors


    【解决方案1】:

    我建议阅读有关 JSF 的教程。因为你的JSF基础好像有问题!

    现在回到你的问题:

    1. Student 类应该是 DataModel 类而不是 managedBean 类
    2. 根据您在 web.xml 中对 FacesServlet 模式的配置,您应该运行应用程序而不是单个文件。启动容器(Tomcat)后,调用您项目的 url:即 localhost:8080/Testing/faces/test.xhtml
    3. 在你的 xhtml 中有一个语法错误:&lt;:inputText value="#{student.slastName}" /&gt;,它应该以 &lt;h:... 开头
    4. 托管 bean 应该有一个范围(applicationScoped、SessionScoped、ViewScoped、...等),但您没有在 bean 中声明它
    5. managedBean 的私有字段应该有 getter/setter,只有 public 可以从外部访问(在 view/xhtml 内)

    这里是一个例子:

    数据模型类:

    package a.b.model;
    
    import java.io.Serializable;
    
    public class Student implements Serializable{
        private static final long serialVersionUID = -7013884476678061379L;
        private String firstName;
        private String lastName;
    
        public Student() {
    
        }
    
        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;
        }    
    }
    

    ManagedBean:

    package a.b.controler;
    
    import java.io.Serializable;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.event.AjaxBehaviorEvent;
    import a.b.model.Student;
    
    @ManagedBean
    @SessionScoped
    public class StudentManager implements Serializable {
        private static final long serialVersionUID = -6254452297683880945L;
        private Student student;    
        public StudentManager(){
    
        }   
        @PostConstruct
        public void init(){
            System.out.println("Called once on Object init!");
            this.student = new Student();
        }
        public final void save (AjaxBehaviorEvent event){
            System.out.println("Ajax command ....");
            System.out.println("Firstname: "+this.getStudent().getFirstName());
            System.out.println("Lasttname: "+this.getStudent().getLastName());
        }
    
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        }
    }
    

    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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Testing</display-name>
      <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>
      <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
      </context-param>
    </web-app>
    

    视图test.xhtml

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <meta charset="UTF-8" />
        <title>Student Registration</title>
    </h:head>
    <body>
        <h:form>
                 First Name: <h:inputText value="#{studentManager.student.firstName}" />
            <br />
            <br />
                 Last Name: <h:inputText value="#{studentManager.student.lastName}" />
            <br />
            <br />
            <h:commandButton value="Submit">
            <f:ajax execute="@form" listener="#{studentManager.save}"/>
            </h:commandButton>
        </h:form>
    </body>
    </html>
    

    启动Container(Tomcat)后,在浏览器中这样打开:

    http://localhost:8080/Testing/faces/test.xhtml
    

    【讨论】:

    • 您是否阅读了 OP 遇到的错误?所有这些都与 JSF 无关。 OP 完全搞砸了 IDE 配置。我编辑了问题以减少不相关的噪音,以便更好地集中注意力。
    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多