未解决的编译问题:无法解析java.lang.String类型。隐式构造函数Object()未定义

时间:2015-11-30 23:05:34

标签: java eclipse compiler-errors

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

Sub SetCellValueToZero()
  Dim last_used_cell As Range, r As Range

  For Each r In Selection.Rows
    Set last_used_cell = ActiveSheet.Cells(r.Row, ActiveSheet.Columns.Count).End(xlToLeft)
    last_used_cell = "0"
    last_used_cell.Offset(0, -1) = "0"
  Next r
End Sub

我得到的错误是:

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

 }

如果我将鼠标悬停在String上,我会看到

  

无法将字符串解析为类型

如果我将鼠标悬停在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) (无法构造函数)上,我会看到:

  

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

我不确定为什么它不会让我在m Student类中使用私有String。 任何人都可以解释这个或给我一些建议吗?

1 个答案:

答案 0 :(得分:1)

我建议阅读有关JSF的教程。因为你似乎在JSF的基础知识方面存在问题!

现在回到你的问题:

  1. Student class应该是DataModel类,而不是managedBean类
  2. 根据您在web.xml中的FacesServlet模式的配置,您应该运行应用程序而不是单个文件。启动Container(Tomcat)后,调用您的项目的url:即localhost:8080 / Testing / faces / test.xhtml
  3. 在您的xhtml中,您遇到语法错误:<:inputText value="#{student.slastName}" />,它应以<h:...
  4. 开头
  5. 一个managedbean应该有一个范围(applicationScoped,SessionScoped,ViewScoped,...等)你没有在你的bean中声明这个
  6. managedBean的私有字段应该有getter / setter,只能从外部访问public(在view / xhtml中)
  7. 这里有一个例子:

    DataModel类:

    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