Struts 2: default action is not found

I changed a lot configuration in struts.xml and web.xml and after a while the welcome-file tag was ignored by Struts 2 and i got

ERROR Dispatcher:38 - Could not find action or result
/att/
There is no Action mapped for namespace / and action name . - [unknown location]
	at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)

when I called the app just by its domain (e.g. http://www.myapp.com). Just the usage of the action itself worked properly (e.g. http://www.myapp.com/showLogin.action).

Solving this issue took me days (’cause of the existing workaround) but it was just a copy- & past-thing in combination with wrong configured examples. Where ever you read about Struts 2 examples a web.xml looks like this:

<!-- Default -->

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

If you change the url-pattern of the filter-mapping from /* to *.action everthing works as supposed.

Spring 3: Spring-Security

As I currently evaluate some frameworks and how the can be used in further projects I thought ‘let’s have a look at Spring security for authentication and authorization’. You need just some simple steps to include Spring security into your Spring project.

First of all you need the necessary lib. In a Maven project just add the following dependency to your pom.xml:

<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-core</artifactId>
   <version>${org.springframework_spring_version}</version>
</dependency>

<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-web</artifactId>
   <version>${org.springframework_spring_version}</version>
</dependency>

(${org.springframework_spring_version} is set to ’3.0.5.RELEASE’ so I just have one version information for all Spring libs)

As second step you need a security context configuration file in your classpath e.g. security-applicationContext.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <http use-expressions="true">
        <intercept-url pattern="/**" access="permitAll" />
        <form-login />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="rod" password="koala" authorities="supervisor, teller, user" />
                <user name="dianne" password="emu" authorities="teller, user" />
                <user name="scott" password="wombat" authorities="user" />
                <user name="peter" password="opal" authorities="user" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

The third step is to tell your web application to use the security filter. This is made within the web.xml. The context-param has to be edited like this:

<context-param>
   <param-name>contextConfigLocation</param-name>
   	<param-value>
   	 classpath:applicationContext.xml
   	 classpath:security-applicationContext.xml
   </param-value>
</context-param>

… and the filter has to be added:

<filter>
   <filter-name>springSecurityFilterChain</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
   <filter-name>springSecurityFilterChain</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

That’s it. BUT you will get the following error message when starting your web server (like Jetty or Tomcat):

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security] Offending resource: ServletContext resource [classpath:spring-applicationContext.xml]

It took me hours to solve this issue. The error message better should say, ‘You are missing a dependency…’      :/       ’cause adding

<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-config</artifactId>
   <version>${org.springframework_spring_version}</version>
</dependency>

to your pom.xml solves the problem.

Now you are ready to use Spring security in your web project.

Spring 3 and JUnit 4 – How To

To use Spring components like services within JUnit you need to tell JUnit to run in a Spring context in order to use Spring configured beans (doesn’t matter whether they are configured by annotation or XML).

Maven pre-requisites: JUnit and Spring-Test in your Spring project’s pom.xml

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.0.5.RELEASE</version>
</dependency>

After setting this up, all you need to do is tell JUnit to run with another runner class and where to Spring configuration is stored:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class TestdatenGenerator

The configuration file has to be located in src/test/resources. Without the @ContextConfiguration-annotation the configuration file has to be named after the class you want to test (e.g. TestdatenGenerator_context.xml). This means you can configure a different Spring context for every test class. But you are also able to inherit the Spring configuration (from your project configuration or other test configurations). To achieve this the applicationContext-test.xml could be look like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<import resource="applicationContext.xml" />

</beans>

If configured as seen above you are able to use injected and Spring instantiated resources as you are used to:

@Resource
private MandantenService mandantenService;
[...]
@Test
public void doSomething() {
    Mandant persMandant = mandantenService.findByName(mandant.getName());
}
Follow

Get every new post delivered to your Inbox.