Trying to include simple css file to a vaadin route
Using spring boot and have a maven project.
When i load the page on the client i get this error:
Refused to apply style from
'http://localhost:8080/frontend/css/msas_login_page.css' because its MIME
type ('text/html') is not a supported stylesheet MIME type, and strict MIME
checking is enabled.
And when i try to access the above url, i get redirected to this html error page
Could not navigate to 'css/msas_login_page.css'
Reason: Couldn't find route for 'css/msas_login_page.css'
Here's my code:
package com.msas.MSAS.UIControllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.context.annotation.SessionScope;
import com.msas.MSAS.UIControllers.Authentication.MSASLoginForm;
import com.vaadin.flow.component.dependency.StyleSheet;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.Route;
#Route("loginPage")
#VaadinSessionScope
#StyleSheet("css/msas_login_page.css")
public class MSASLoginPage extends HorizontalLayout implements
AfterNavigationObserver {
private static final long serialVersionUID = 8673461297922218502L;
private MSASLoginForm loginForm;
private VerticalLayout container;
public MSASLoginPage(#Autowired MessageSource messageSource) {
super();
this.initComponents(messageSource);
}
private void initComponents(MessageSource messageSource) {
this.loginForm = new MSASLoginForm(messageSource);
this.container = new VerticalLayout();
this.container.setDefaultHorizontalComponentAlignment(Alignment.CENTER);
this.container.add(this.loginForm);
this.addClassName("login-page-container");
this.setDefaultVerticalComponentAlignment(Alignment.CENTER);
this.setHeightFull();
this.add(this.container);
}
#Override
public void afterNavigation(AfterNavigationEvent event) {
boolean isError = event.getLocation().getQueryParameters()
.getParameters().containsKey("error");
this.loginForm.setError(isError);
}
}
Here's my project structure:
src
main
java
...
resources
...
webapp
frontend
css
msas_login_page.css
This is css file content:
.login-page-container{
}
Fixed the problem.
I was using a multi-modules maven project, and the main method which contains the #SpringBootApplication and which runs the entire application was not in the same module where the vaadin frontend folder.
Solution was to move the main class to the same maven module as the one used for vaadin.
Related
I have the following setup:
Java Play framework version 2.8.13
sbt.version=1.3.13
routes file:
-> /v1/bar Foo.Routes
Foo.routes file:
GET /foo controllers.FooController.index(request: Request)
FooController file:
package controllers;
import play.mvc.Controller;
import play.mvc.Http.Request;
import play.mvc.Result;
public class FooController extends Controller {
public Result index(Request request) {
return ok("hello world");
}
}
FooTests file:
package test;
import static org.junit.Assert.assertEquals;
import controllers.routes;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FooTests {
private static final Logger LOG = LoggerFactory.getLogger(FooTests.class);
#Test
public void test001() {
var fooUrl = routes.FooController.index().url();
LOG.debug("fooUrl: {}", fooUrl);
assertEquals("/v1/bar/foo", fooUrl); // /foo fail
}
#Test
public void test002() {
var fooUrl = new ReverseFooController(() -> "/v1/bar").index().url();
LOG.debug("fooUrl: {}", fooUrl);
assertEquals("/v1/bar/foo", fooUrl); // ok
}
}
How can I get in my unit tests the full url (/v1/bar/foo) of the controller method ?
Reversing the controller (test001()) does not work, it returns only the path from it's containing Foo.routes file (/foo).
The only solution I found is instantiating the reverted controller (test002()), but I don't like hard coding the prefix like this. It defeats the whole purpose of reverting routes to get the url.
Can I dynamically retrieve the full url (/v1/bar/foo) of the controller inside my unit tests having this split routes strategy ?
I put the source in a repo here
I get a vaadin project looking like this:
package com.example.myapplication;
import com.vaadin.annotations.StyleSheet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.label.LabelState;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Label;
#StyleSheet ("stylesheet.css")
#Theme("mytheme")
public class MyUI extends UI {
private static final long serialVersionUID = 1L;
#Override
protected void init(VaadinRequest vaadinRequest) {
Label lb = new Label("VAADIN PROJECT");
lb.setStyleName("mystyle");
layout.addComponent(lb);
setContent(layout);
}
#WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
#VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
And a .css file that is situated in the same directory as the main file:
.mystyle {
color: red;
font-size: 180px;
pading-left: 30px;
}
When i'am trying to run the project,it's print only the text with no more changes. How can i fix it ?
MY RESULT
You can have a look at the basic starter of Vaadin. It imports a CSS file for the overall view and a CSS file for the specific view.
You can see that HelloWorldView has the annotation #CssImport("./styles/views/helloworld/hello-world-view.css"). This results in importing a CSS file only for this view.
The class MainView forms the parent/border for the other views. It also imports a CSS file with specific attributes for the MainView with the annotation #CssImport("./styles/views/main/main-view.css").
The respective files can be found in the frontend folder of the application.
I develop a test for the mobile application using Cucumber+Junit+Appium. When I try to run a test using cucumber and JUnit runner I receive: io.cucumber.junit.UndefinedStepException: The step "I install the application" is undefined. You can implement it using the snippet(s) below:
I tried some of the solutions from the medium blog and stack question, but this doesn't help.
I have a project structure:
src
|-main
|--java
|---{project-name}
|----config
|----models
|----screens
|----services
|-test
|--java
|---{project-name}
|----helpers
|----stepDefinitions
|-----LoginStep.java
|-----BaseStep.java
|-----LoginStep.java
|----RunCucumber.java
|--resources
|---feature
|----Login.feature
RunCucumber.java
package com.mobile.automation.framework;
import com.google.inject.Guice;
import com.mobile.automation.framework.module.ServiceModules;
import com.mobile.automation.framework.service.AppiumServer;
import com.mobile.automation.framework.config.drivers.DriverFactory;
import com.mobile.automation.framework.module.ScreensModule;
import io.appium.java_client.AppiumDriver;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(
strict = true,
monochrome = true,
glue = "src.test.java.com.mobile.automation.framework.stepDefinition",
features = "src/test/resources/feature",
plugin = {"pretty", "html:target/cucumber-report/cucumber.html",
"json:target/cucumber-report/cucumber.json",
"junit:target/cucumber-report/cucumber.xml"})
public class RunCucumber {
public static AppiumDriver driver;
#Before
public void setUpDriver() {
init();
new AppiumServer().startServer();
driver = new DriverFactory().getDriver();
}
#After
public void tearDownDriver() {
if (driver != null) {
driver.quit();
new AppiumServer().stopServer();
}
}
private void init() {
Guice.createInjector(
new ScreensModule(driver),
new ServiceModules(driver)
).injectMembers(this);
}
}
Login.feature
Feature: Sign In feature
Background:
Given I install application
And I enable all network activity
Then I am on Sign Page
Scenario: Sign In scenario
Given I am go to the Login Page
And I fill valid user data using "Config"
And I click sign in button
Then I am login in the application
LoginStep.java
package com.mobile.automation.framework.stepDefinition;
import javax.inject.Inject;
import com.mobile.automation.framework.config.ProjectConfig;
import com.mobile.automation.framework.models.User;
import com.mobile.automation.framework.screens.DashboardScreen;
import com.mobile.automation.framework.screens.SignInScreen;
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* #author Tomash Gombosh
*/
public class LoginStep {
#Inject
private SignInScreen signInScreen;
#Inject
private DashboardScreen dashboardScreen;
#Given("^I am go to the Login Page$")
public void iAmGoToTheLoginPage() {
dashboardScreen.tapLogin();
}
#And("I fill valid user data using {string} {string}")
public void iFillValidUserDataUsing(String userName, String password) {
signInScreen.fillLogin(userName, password);
}
#And("I fill valid user data using {string}")
#ParameterType("Config")
public void iFillValidUserDataUsing() {
signInScreen.fillLogin(new User(data -> {
data.setEmail(new ProjectConfig().getBaseUser());
data.setPassword(new ProjectConfig().getBaseUserPassword());
}));
}
#And("I click sign in button")
public void iClickSignInButton() {
signInScreen.clickLogin();
}
#Then("I am login in the application")
public void iAmLoginInTheApplication() {
assertThat(signInScreen.isDisplayed()).isEqualTo(true);
}
}
Some of the steps on the Login class is missing, but that is because I want to put all the code to the question.
I expected to run that feature, but actually that is not work.
Typically src.test.java is not part of the package name. Try using:
glue = "com.mobile.automation.framework.stepDefinition",
And because Cucumber will search the runners package for glue by default can also remove the glue entirely.
New here myself working on Junit Runner, I agree with #M.P.Korstanje with classpath. Although I just had to change glue to refer the stepDefs with classpath too - wasn't getting recognized before.
So basically this is what I did to catch the necessary files to be triggered
feature = { "classpath:path-from-repo-root.feature" },
glue = { "classpath:reference-to-stepDef-folder" }
Bear in mind: for glue - I used to the reference to the folder containing the stepDefs and not the stepDef file itself. Hope this helps someone. Thanks
The problem seems to be some poor error message.
I've created a dynamic web app project in eclipse and the runtime is TomEE+ 1.7.2
So this is the publisher
package study;
import java.io.IOException;
import java.net.InetSocketAddress;
import javax.ws.rs.ext.RuntimeDelegate;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class StandaloneJaxRsServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(
"localhost", 8765), 8);
HttpHandler requestHandler = RuntimeDelegate.getInstance()
.createEndpoint(new JaxRSApplication(), HttpHandler.class); //<<<<< line 15
server.createContext("/jaxrs/", requestHandler);
server.start();
}
}
this is the app
package study;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath(value="/jaxrs")
public class JaxRSApplication extends Application{
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<>();
set.add(NotSingletonResource.class);
return set ;
}
//
// #Override
// public Set<Object> getSingletons() {
// Set<Object> set = new HashSet<>();
// set.add(new SingletonResource());
// return set ;
// }
}
and this is the resource
package study;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/notSingletonResource")
public class NotSingletonResource {
private volatile int counter = 0;
#GET
#Produces({MediaType.TEXT_HTML})
public String getHello() {
return "Not singleton resource " + counter++;
}
}
when I try to run StandaloneJaxRsServer.main() I get
Exception in thread "main" java.lang.IllegalArgumentException
at org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl.createEndpoint(RuntimeDelegateImpl.java:104)
at study.StandaloneJaxRsServer.main(StandaloneJaxRsServer.java:15)
However, IllegalArgumentException does not tell much. What am I doing wrong?
The javadoc for the RuntimeDelegate interface states "Throws: IllegalArgumentException - if application is null or the requested endpoint type is not supported.' For a JAX-RS Application, I think you're supposed to use org.apache.cxf.jaxrs.JAXRSServerFactoryBean, and use the result of creating the endpoint to set up a server.
I won't accept my own answer, I just want to keep here some useful information just in case someone needs it.
TomEE+ does not have the libraries to make this work. Instead, what I've did was to remove all the TomEE+ dependencies from my eclipse project, then I've converted it to a maven project just to include this dependency according to #lmiguelmh answer to this question:
Latest Jersey example does not work
Then I've just added these jersey libraries
I'll wait a little bit more before accepting an answer here, because I really would like a better answer. I am following the steps described in the book "Java Web Services, Up and Running", so I think people will really need to know this information.
I am new to the Play framework and Java in general. What is wrong with this Global.java file? I get the error no interface expected here on the line public class Global extends GlobalSettings {
import play.*;
import play.libs.*;
import com.avaje.ebean.Ebean;
import models.*;
import java.util.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import play.api.Application;
import play.api.GlobalSettings;
public class Global extends GlobalSettings {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
#Override
public void onStart(Application app) {
if(User.find.findRowCount() == 0){
Ebean.save((List) Yaml.load("initial-data.yml"));
}
//Start Spring WS framework
applicationContext.start();
}
#Override
public void onStop(Application app) {
applicationContext.stop();
}
}
I am trying to create a simple SOAP web service within Play Java using the Spring framework. Perhaps I am going about this the wrong way?
Remove the play.api.GlobalSettings import. Do the same for play.api.Application. These...
import play.api.Application;
import play.api.GlobalSettings;
It looks like your project has defaulted to a Scala project rather than a Java one, I think. You should be using the play.GlobalSettings and play.Application objects for a Java Play application, covered by your current play.* import.