<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Testing</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<!-- Spring application context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-managers.xml
/WEB-INF/applicationContext-beans.xml
</param-value>
</context-param>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>laguna</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>
<!-- Enabling facelets: Use Documents Saved as *.xhtml -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<!-- Special Debug Output for Development -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<!-- Defining and mapping the RichFaces filter -->
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Don't show XHTML files unless user is in developer role -->
<security-constraint>
<display-name>Restrict XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>Only Let 'developer's access XHTML pages
</description>
<role-name>developer</role-name>
</auth-constraint>
</security-constraint>
</web-app>
Tags in my blog
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
09 May 2009
Example web.xml Spring+JSF+RichFaces+Facelets
Wrong web.xml namespace version
Last time I troubled again with wrong namespace version in eclipse. I got error like this on eclipse:
"CHKJ3020E: Invalid Security role-name: xxxx"
The solution is just replace your web.xml namespace to version 2.5:
"CHKJ3020E: Invalid Security role-name: xxxx"
The solution is just replace your web.xml namespace to version 2.5:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Restrict files accessibility from web.xml
Example to restrict access to facelet .xhtml files from web.xml:
<!-- Don't show XHTML files unless user is in developer role -->
<security-constraint>
<display-name>Restrict XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>Only Let 'developer's access XHTML pages
</description>
<role-name>developer</role-name>
</auth-constraint>
</security-constraint>
RichFaces web.xml
I love this framework, this JSF component already using ajax technology. This is example web.xml to use RichFaces that also using laguna skin and facelet:
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<!-- Defining and mapping the RichFaces filter -->
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>laguna</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>
<!-- Use Documents Saved as *.xhtml -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<!-- Special Debug Output for Development -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
JSF Implementation
There are 2 JSF implementations that I ever try to use. There are Mojarra (seperated project to use JSF implementation outside Glassfish Application Server) and Apache MyFaces from ASF.
These are minimum required files for Mojarra:
- jsf-api.jar
- jsf-impl.jar
that could be downloaded from here.
These are minimum required files for Apache MyFaces:
- myfaces-api-1.2.6.jar
- myfaces-impl-1.2.6.jar
could be downloaded from here.
When I started my web app with tomcat that could run already using Mojarra and change the implementation to MyFaces, the framework asked me to add 2 more files:
- commons-discovery-0.4.jar, and
- commons-codec-1.3.jar
In the web.xml i do not need to change anything. This is part of my web.xml:
These are minimum required files for Mojarra:
- jsf-api.jar
- jsf-impl.jar
that could be downloaded from here.
These are minimum required files for Apache MyFaces:
- myfaces-api-1.2.6.jar
- myfaces-impl-1.2.6.jar
could be downloaded from here.
When I started my web app with tomcat that could run already using Mojarra and change the implementation to MyFaces, the framework asked me to add 2 more files:
- commons-discovery-0.4.jar, and
- commons-codec-1.3.jar
In the web.xml i do not need to change anything. This is part of my web.xml:
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
19 April 2009
Spring getting started
To start using spring just add this two jars:
- spring.jar
- commons-logging.jar
Put this spring xml bean configuration file with .xml extension. For example beans.xml.
Load the xml bean configuration by using this syntax in your java class:
You can get your object using that context:
- spring.jar
- commons-logging.jar
Put this spring xml bean configuration file with .xml extension. For example beans.xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="beanName" class="path.to.ClassName">
<property name="propName" value="1"/>
<property name="otherPropName" value="2"/>
</bean>
</beans>Load the xml bean configuration by using this syntax in your java class:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");You can get your object using that context:
ClassName beanName = (ClassName) context.getBean("beanName");
16 April 2009
Send email with JavaMail
Require to download mail.jar and activation.jar from JavaMail library.
Example:
Main.java
package sendmail;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author Stefan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String host = "mail.hostname.com";
String from = "user1@hostname.com";
String to = "user2@hostname.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
Authenticator auth = new MyAuth();
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
try {
Address addressFrom = new InternetAddress(from);
Address addressTo = new InternetAddress(to);
message.setText("Hello");
message.setSubject("First");
message.setFrom(addressFrom);
message.addRecipient(RecipientType.TO, addressTo);
Transport.send(message);
} catch (MessagingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
MyAuth.java
package sendmail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
*
* @author Stefan
*/
public class MyAuth extends Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user1@hostname.com", "password");
}
}
Example:
Main.java
package sendmail;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author Stefan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String host = "mail.hostname.com";
String from = "user1@hostname.com";
String to = "user2@hostname.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
Authenticator auth = new MyAuth();
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
try {
Address addressFrom = new InternetAddress(from);
Address addressTo = new InternetAddress(to);
message.setText("Hello");
message.setSubject("First");
message.setFrom(addressFrom);
message.addRecipient(RecipientType.TO, addressTo);
Transport.send(message);
} catch (MessagingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
MyAuth.java
package sendmail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
*
* @author Stefan
*/
public class MyAuth extends Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user1@hostname.com", "password");
}
}
13 April 2009
How to use JUnit
To perform unit testing using JUnit, simply just add
You can run your testing class by invoking main method from JUnitCore like this:
If you expect certain Exception have to thrown by a JUnit test method, you can annotate the method like this:
@org.junit.Test annotation before the test method.You can run your testing class by invoking main method from JUnitCore like this:
java org.junit.runner.JUnitCore TestClass1 TestClass2 ...If you expect certain Exception have to thrown by a JUnit test method, you can annotate the method like this:
@Test(expected = IndexOutOfBoundsException.class) public void empty() {
new ArrayList<object>().get(0);
}
How to create JavaDB database embeded and client/server
Embeded Mode
Run locally:
To connect from java:
Client/Server Mode
Start the server:
Start the client:
To connect from java:
Run locally:
java -jar derbyrun.jar ij;
ij> connect 'jdbc:derby:firstdb;create=true';To connect from java:
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName="jdbcDemoDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";Client/Server Mode
Start the server:
java -jar derbyrun.jar server startStart the client:
java -jar derbyrun.jar ij
ij> connect 'jdbc:derby://localhost:1527/seconddb;create=true';To connect from java:
String driver = "org.apache.derby.jdbc.ClientDriver";
...
String connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";create=true";
Subscribe to:
Posts (Atom)
