Tag Archive 'struts'

Jun 14 2008

Capturing log-in event in Acegi

Published by under IT,Java

Adding some custom code that is supposed to be launched while user logs in it isn’t available out of the box but it can be done easily. In order to do this you have to implement interface org.springframework.context.ApplicationListener.

public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AuthenticationSuccessEvent) {
        AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;
        String username = authEvent.getAuthentication().getName();
        // Any custom logic
}
}

Such listener has to be registered. In order to do this – just create a bean in xml configuration file where other Acegi related beans are declared (usually security.xml or applicationContext-security.xml).

<bean class="my.package.SomeExampleListener">
    <property name="someExampleManager" ref="someExampleManager"></property>
</bean>

You can inject any required dependencies here.

That’s all!

One response so far

Feb 12 2008

AppFuse – first impression

Published by under Java

Couple days ago I was looking for some starter kit for Java web applications based on Spring framework. After short investigation I have decide to try some of Maven2 archetypes. If you just type: mvn archetype:create you get list of standard archetypes. Take a look on positions 1-9 (AppFuse starter-kits).

For further investigation I chose appfuse-modular-struts which is a solution based on Hibernate, Spring and Struts2.

To kickoff application kickoffs there are just few simple steps:

  • Launch database. I was trying it with Derby and PostgreSQL.
  • Configure database settings in pom.xml in main directory by adding line true in ‘profiles’ section after ‘id’ of DB of your choice and setting proper user/pass and database name if applicable.
  • Kickoff Maven build and run application:
    mvn
    cd web
    mvn jetty:run-war
  • Navigate to http://localhost:8080 in you browser.

You will see simple application that allows to:

  • Log in (admin/admin).
  • List the users.
  • Edit user data.

Everything looks great. That is the moment when you are starting to look for sources of this application. To get the source you need to invoke another Maven target
mvn appfuse:full-source.

Now you can rebuild the application and do the changes you want.
There is also nice feature for adding standard code. You can generate all required class and configurations for domain objects. To do this add new domain object in your.application.model package in core module and annotate it properly. After that:
cd core
mvn appfuse:gen -Dentity=YourNewObject
cd ..
cd web
mvn appfuse:gen -Dentity=YourNewObject

All the classes, configurations and webpages should be ready for performing CRUD operations for this POJO!

Everything looks great, but there is one concern. Don’t use Java 1.6. Use Java 1.5. It took me several hours to figure out that this is the problem while tests fails (just HTTP 500 respond, no exceptions, no logs). Haven’t investigated yet what’s it the core of the problem with using it on Java 6.

AppFuse homepage: http://appfuse.org/display/APF/Home

No responses yet

May 04 2007

Injecting EJB 3.0 beans into Struts 2.0

Published by under IT,Java

EJB 3.0 provides easy bean injection. While developing web layer in Struts 2.0 you can use similar mechanism as well. Struts 2.0 is using Spring libraries for implementing Inversion of Control design pattern.

EJB 3.0 is based on using Java 1.5 annotations. It simplifies deployment process.

Let define example Session Bean.

@Stateless(mappedName=”ejb/EmployeeManager”)
public class EmployeeManagerSB implements IEmployeeManager {

public Long createAccount(String loginName) {
// Implementation of creating an account…
}

}

It implements interface:

@Remote
@Local

public interface IEmployeeManager {

Long createAccount(String loginName);

}

To use EmployeeManager session bean in Struts 2.0 reference to this object has to be retrieved.

Let define Struts action class which will use EmployeeManager session bean.

@Validation()
public class NewEmployeeAction extends ActionSupport {

private IEmployeeManager employeeManager;
private String loginName;
private long id;
private String redirectURL;

public String execute() throws Exception {
Long id = this.employeeManager.createAccount(this.loginName);
this.setId(id.longValue());
return SUCCESS;
}

public IEmployeeManager getEmployeeManager() {
return employeeManager;
}

public void setEmployeeanager(IEmployeeManager employeeManager) {
this.employeeManager = employeeManager;
}

public String getLoginName() {
return loginName;
}

@RequiredStringValidator(message = “Please enter a login name”, trim = true)
public void setLoginName(String loginName) {
this.loginName = loginName;
}

public long getId() {
return id;
}

public String getRedirectURL() {
return redirectURL;
}

public void setRedirectURL(String redirectURL) {
this.redirectURL = redirectURL;
}

}

Framework is taking care of injecting proper object while creating this action object. In Struts 2.0 the only thing that has to be done to do this is defining it in applicationContext.xml configuration file.

<jee:jndi-lookup id=”employeeManager” name=”ejb/EmployeeManager“>

<bean id=”newEmployeeAction” class=”pl.edu.agh.sius2007.jspynet.struts.NewEmployeeAction”>
<property name=”employeeManager” ref=”employeeManager”>
</bean>

The only configuration in Struts is defining two beans.

  • employeeManager which will be retrieved from JNDI;
  • newEmployeeAction which is Struts action.

employeeManager bean will be retrieved automatically and injected into newEmployeeAction.

No responses yet