42¢ glxn.net

Simple CDI/WELD login example

View

<h:form id="spotifylogin" class="loginform spotifyform">
    <h2>Spotify login</h2>
    <p>Please log in to your spotify account</p>

    <h:outputLabel for="spotifyusername" value="Username:"></h:outputLabel>
    <h:inputText id="spotifyusername" value="#{spotiWelder.username}"></h:inputText>
    <h:message for="spotifyusername" errorClass="invalid"></h:message>

    <h:outputLabel for="spotifypassword" value="Password:"></h:outputLabel>
    <h:inputSecret id="spotifypassword" value="#{spotiWelder.password}"></h:inputSecret>
    <h:message for="spotifypassword" errorClass="invalid"></h:message>

    <h:commandButton id="login" action="#{spotifyWeb.login}" value="Log in!"></h:commandButton>
</h:form>

Entity

@Entity
public class SpotiWelder implements Serializable {

    private static final long serialVersionUID = -6625241632523446019L;

    private Long id;

    private String username;

    private String password;

    public SpotiWelder() {
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NotNull
    @NotEmpty
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @NotNull
    @NotEmpty
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Action Bean

@SessionScoped
@Named
public class SpotifyWeb implements Serializable {

    private static final long serialVersionUID = -352191768337315102L;

    private Logger log;

    SpotiWelder spotiWelder = new SpotiWelder();

    @Inject
    FacesContext facesContext;

    public SpotifyWeb() {
        this.log = Logger.getLogger(this.getClass().getSimpleName());
        connection = new SpotiWeldConnection();
    }

    @PostConstruct
    public void initialize() {
        log.info(this.getClass().getSimpleName() + " was constructed");
    }

    public void login() {
        connection.login(spotiWelder.getUsername(), spotiWelder.getPassword());

        log.info("succesful login for " + loggedInUser);
        facesContext.addMessage(null, new FacesMessage("Welcome " + loggedInUser.getName()));
    }

    @Produces @Named
    public SpotiWelder getSpotiWelder() {
        return spotiWelder;
    }
}

Related:
http://seamframework.org/Community/JPAEntityIsUnknownWhenUsingTheModelAnnotation
http://www.seamframework.org/Community/ErrorWhenUsingSessionScopedAndEntityAnnotation

comments powered by Disqus

Moar stuffs

07 Jun 2015 Deploy an Ember app to gh-pages using npm run-script
06 Jun 2015 JSON Contract testing using unit tests to assert full stack integration across REST services
03 May 2015 simple http serve a directory from terminal
07 Jan 2014 civu, a CLI for cloning git repositories from jenkins views
06 Jan 2014 PyramidSort, a Sublime Text plugin for for reformatting text
05 Jan 2014 Git commit-message hook for JIRA issue tags
31 May 2013 hacking kitchen tiles with coffeescript
30 May 2013 Nuke, ps grep kill something
24 May 2013 mvnr: recursive mvn command runner
23 May 2013 Query By Example for JPA
22 May 2013 gitr: recursive git command runner
21 May 2013 Keeping gh-pages branch in sync with master
19 May 2013 Migrated from wordpress to jekyll and github pages
14 Aug 2012 Using Sublime Text 2 as git commit message editor
10 Mar 2012 QRGen, a small wrapper on top of ZXING for generating QRCodes in java
04 Jan 2012 My Bash PS1 with git branch info
17 Aug 2010 Making a swing project using IntelliJ IDEA GUI builder with maven, Including executable jar
01 May 2010 Using Arquillian to test against a remote jboss container from within IDEA
06 Apr 2010 WELD/CDI lightningtalk from Know IT 2010 annual conference
03 Apr 2010 Solving Sudoku using java swing and junit
01 Mar 2010 Simple CDI/WELD login example
01 Mar 2010 Implementing @RequestParam in CDI/WELD using Qualifier and InjectionPoint as @HttpParam
01 Nov 2009 Seam Maven Refimpl