I just downloaded the play framework from their site and am working through this tutorial.
I've noticed the framework creates the folders app/controllers and app/views, but not a models folder. I created it manually and added Task.java to it. When I get to the section entitled "Rendering the first page" and open localhost:9000/tasks I get a compilation error that says package play.models does not exist. Here is what my Task.java looks like:
package models;
import java.util.*;
public class Task {
public Long id;
#Required
public String label;
public static List<Task> all() {
return new ArrayList<Task>();
}
public static void create(Task task) {
}
public static void delete(Long id) {
}
}
Here is application.java, the file generating the compilation error:
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import play.data.*;
import play.models.*; // COMPILATION ERROR: "package play.models does not exist"!
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I believe it's supposed to be import models.Task; as opposed to import play.models.*;
That's quite confusing (IMHO) step in this tutorial, instead scroll down to Persist the tasks in a database section which describes preparing a model to cooperate with DB :) (it extends Model class, uses proper annotations, etc)
As you recognized it yet, you need to create a models package yourself.
Also as cYn wrote: you should import models like models.SomeModel into your controller
You are correct HukeLau_DABA , the Play will not create the models package for you. you have to create it.
I got these imports in my Application controller class. I got this sample play application running.
import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms._
import models.Task
and another thing in Eclipse is it will not import the necessary imports automatically.
it is bit pain now, once the IDE support get better I hope this will change.
Related
I have two maven projects "Bonita-engine"and "activity-engine". these projects are the code source of two BPM engine. My project is to find the common method of these two engine code sources.So I create an API Java to call this Java method.
as these methods are on a different project I can not call this method. in fact, I added this two project to the library of my API Java but it doesn't work.
call method getDescription() from bonita-engine maven project
* Copyright (C) 2015 BonitaSoft S.A.
package org.bonitasoft.engine.bpm.process.impl.internal;
import java.util.Date;
import org.bonitasoft.engine.bpm.internal.NamedElementImpl;
import org.bonitasoft.engine.bpm.process.ProcessInstance;
/**
* #author Baptiste Mesta
* #author Matthieu Chaffotte
* #author Celine Souchet
*/
public class ProcessInstanceImpl extends NamedElementImpl implements ProcessInstance {
#Override
public String getDescription() {
return description;
}
}
call method getDescription() from activiti-engine maven project
package org.activiti.engine.impl.persistence.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.bpmn.data.IOSpecification;
import org.activiti.engine.impl.context.Context;
public class ProcessDefinitionEntityImpl extends AbstractEntity implements ProcessDefinitionEntity, Serializable {
public ProcessInstanceImpl(final String name) {
super(name);
}
public String getDescription() {
return description;
}
}
API JAVA : call the common method from the two maven project
import org.activiti.bpmn.model.*;
import org.bonitasoft.engine.bpm.process.impl.internal.* ;
import org.bonitasoft.engine.bpm.*;
import java.util.*;
import java.util.Date;
import org.bonitasoft.engine.bpm.internal.*;
import org.bonitasoft.engine.bpm.process.* ;
public class apicommon {
public activitiProcess = new ProcessDefinitionEntityImpl() ;
public String name;
public bonitaProcess = new ProcessInstanceImpl(name) ;
public enum bpm {
activiti , bonita
}
bpm chose ;
public apicommon() {
}
public String getProcessDescription() {
if(chose==bpm.activiti){
return activitiProcess.getDescription() ;
}else if(chose==bpm.bonita){
return bonitaProcess.getDescription();
}
}
i import the package "org.bonitasoft.engine.bpm.process.impl.internal" and the package "org.activiti.engine.impl.persistence.entity" but i can not get access to ProcessInstanceImpl method and ProcessDefinitionEntityImpl method !
#sara, you have to add the 2 projects to the build path of the API java(api common).
Right click on the (api common) java project => Build path => Configure Build Path
Click on the projects tab
Click Add button
add your project1 (bonita) by checking the box next to it
Again add project2 (activiti-engine) usign the same method
Click Apply and OK to close the dialog.
Now your imports should be working.
Edit:
You have not been clear about which import is the problem. More information is necessary to get to the root of the problem.
As regarding the import error, you can Ctrl + click the offending import. This will take you to a page Source Not Found and a button labelled Attach Source. Click the button to search for the location of what I am suspect is an external jar.
package com.test.model.listener;
import org.osgi.service.component.annotations.Component;
import com.google.gson.InstanceCreator;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.*;
#Component(immediate = true, service = ModelListener.class)
public class InsertInstanceModelListener extends BaseModelListener<Instance??> {
#Override
public void onAfterCreate(Instance?? model) throws ModelListenerException {
System.out.println("InsertInstanceModelListener.onAfterCreate()");
super.onAfterCreate(model);
}
}
I'm newbie of liferay.I think it's may be something like this,but don't know how to make it right.
You are on your way. This OSGi component needs to be specific,
you are probably looking for com.liferay.portal.kernel.model.VirtualHost
If I understood correctly.
You do not need to call supper though.
Is there a Extension Point for the Job REST API?
I want to add some information when http://server/jenkins/job/job_name/job_number/api/json is called.
Any hints?
OK, after a lot of research and tries, I've found the answer.
To expose additional data in the Job/Build REST API, the TransientActionFactory (http://javadoc.jenkins-ci.org/jenkins/model/TransientActionFactory.html) needs to be extended using the AbstractBuild (http://javadoc.jenkins-ci.org/hudson/model/AbstractBuild.html) as it type.
You'll have something like this:
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import java.util.Collection;
import java.util.Collections;
import jenkins.model.TransientActionFactory;
#Extension
public class MyTransientActionFactory extends TransientActionFactory<AbstractBuild> {
#Override
public Class<AbstractBuild> type() {
return AbstractBuild.class;
}
#Override
public Collection<? extends Action> createFor(AbstractBuild target) {
return Collections.singleton(new MyAction(target));
}
}
That will add MyAction to the AbstractBuild actions list which is shown in the REST API.
To familirise myself with the Play 2.0 Framework, I've been following the Play 2.0 introduction tutorial. Unforunatly, I can't get the Ebean persistence to work. My Task.java looks like this:
package models;
import java.util.*;
import play.db.*;
import play.data.validation.Constraints.*;
import javax.persistence.*;
#Entity
public class Task {
#Id
public Long id;
#Required
public String label;
public static List<Task> all() {
return find.all();
}
public static void create(Task task) {
task.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
}
But on running the applicaton, find and save can't be resolved. I guess I forgot to import a module, but I can't figure out which on it is ...
I believe you need to extend Model class.
#Entity
public class Task extends Model {
....
}
Hope this helps.
As Dan W. already said, you need to extend the Model class:
#Entity
public class Task extends Model {
....
}
You also have to add the following static methode:
public static Finder<Long,Task> find = new Finder(
Long.class, Task.class
);
Also make sure you added following lines to your application.conf file to enable the database:
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"
Hi I would like to import Play framework Java Exension* into a Play framework Model.
In specific I'd like to have in my model:
package models;
// various import
import play.templates.JavaExtensions;
#Entity
public class Product extends Model {
#PrePersist
public void save_slug(){
slug = title.slugify();
}
}
But I'm receiving following error
The method slugify() is undefined for the type String
What am I doing wrong?
*references:
- http://www.playframework.org/documentation/1.1/javaextensions#aslugifya
- http://www.playframework.org/documentation/api/1.2.4/play%2Ftemplates%2FJavaExtensions.html
Java extensions are static methods of JavaExtensions class, you can use them as follows:
slug = JavaExtensions.slugify(title);