struts2 set <run-as> in web.xml - struts2

So I am trying to migrate from struts1 to struts2 and the project have in servlet
struts1 web.xml:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.servlet.ActionServlet</servlet-class>
...
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
...
<run-as>
<role-name>user</role-name>
</run-as>
</servlet>
But in struts2 we use filter and I am not able to use < run-as>
<filter> ... </filter>
The < run-as> is link to the weblogic.xml and ejb
Can I use servlet in web.xml just for that? If yes, how can I do it?
Or Can I use ejb-ref in web.xml instead?
I am not sure what to do to with this.

Related

Struts2 and Comet url pattern conflict

I am trying to integrate comet chat into my struts2+hibernate application .First I have tested comet chat in separate web application. It works fine .Ihave download it from http://skillshared.blogspot.in/2012/10/facebook-similar-chat-for-your-java-web.html after then I try to implement in my application
My problem in web.xml here is my web.xml file
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<display-name>Cometd Test WebApp</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Portability Filter, needed only to run on non Jetty
or non Servlet-3.0 containers like Tomcat-->
<filter>
<filter-name>continuation</filter-name>
<filter-class>org.eclipse.jetty.continuation.ContinuationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>continuation</filter-name>
<url-pattern>/cometd/*</url-pattern>
</filter-mapping>
<!-- Cometd Servlet -->
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.annotation.AnnotationCometdServlet</servlet-class>
<init-param>
<param-name>timeout</param-name>
<param-value>20000</param-value>
</init-param>
<init-param>
<param-name>interval</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>maxInterval</param-name>
<param-value>10000</param-value>
</init-param>
<init-param>
<param-name>maxLazyTimeout</param-name>
<param-value>5000</param-value>
</init-param>
<init-param>
<param-name>long-polling.multiSessionInterval</param-name>
<param-value>2000</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>transports</param-name>
<param-value>org.cometd.websocket.server.WebSocketTransport</param-value>
</init-param>
<init-param>
<param-name>services</param-name>
<param-value>com.semika.cometd.ChatService</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
from above code application works fine but chat not working. I saw so posts regarding this problem , I have a solution
<URL-pattern>*.action</URL-pattern>
when I am trying to do this chat works fine but application doesn't work. When I have place application only working.
<url-pattern>/*</url-pattern>
I have tried this one also in struts.xml but no use.
<constant name="struts.action.excludePattern" value="/cometd/*,/cometd/.*"/>
What can be the cause of the problem ?
The whole thing depends on your Struts version. If you have an old version, you should migrate to a new version for many reasons, especially security fixes.
However, considering that:
With Struts >= 2.1.3, the FilterDispatcher is deprecated, and the new StrutsPrepareAndExecuteFilter must be used. Read more
With Struts <= 2.1.6, the struts.action.excludePattern doesn't exist. Read more
your code can't possibly work ;)

Trouble integrating prettyfaces with jsf

I need to integrate pretty faces with my jsf 2.0, primefaces application, but its giving some troubles.
As mentioned in the getting started i placed following in my web.xml, added the required jar in the lib folder
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
other items in my web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>org.primefaces.extensions.DELIVER_UNCOMPRESSED_RESOURCES</param-name>
<param-value>false</param-value>
</context-param>
But i am getting following Error:
Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected
If i remove <async-supported> from the project builds, project compiles but the mapping doesnt work.
pretty-config.xml is same as in the getting started.
Do i need to mention in my web.xml the name/path of the mapping file i.e pretty-config.xml?
EDIT:
I am using Glassfish server 3.
It is very important to check the version attribute you are using in your web.xml. If you have version="2.5" set, you have to add this to your web.xml:
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
Please not that <async-supported>true</async-supported> is NOT set here, because it is only supported in Servlet 3.0.
If you have version="3.0" set in your web.xml, you don't have to add anything to your web.xml. In this case PrettyFaces automatically registers the filter using the web-fragment.xml that is included in prettyfaces-jsf2.jar.
You don't have to specify the location of pretty-config.xml anywhere. Just place it in your WEB-INF folder and PrettyFaces will find it.
You should also add one mapping to your pretty-config.xml, so you can check if everything works correctly. If you have for example a page that you typically access using an URL like:
http://localhost:8080/myapp/faces/login.xhtml
Then you can add this mapping:
<url-mapping id="login">
<pattern value="/login" />
<view-id value="/faces/login.xhtml" />
</url-mapping>
Now you should be able to access the page using this link:
http://localhost:8080/myapp/login

Struts2 convention plugin (m2eclipse/geronimo)

Using convention-plugin seems really easy but im not able to make it work :(
I'm using struts2 version 2.2.3
I have a package named com.medicis.actions with a UserAction extending ActionSupport.
I don't have any struts.xml file
I have the convention-plugin depency set on my maven configuration (i also checked the generated war file)
There is my web.xml :
<display-name>Starter</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- Filters -->
<filter>
<filter-name>action2-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>action2-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<!-- Welcome file lists -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I tried with and without the init param for the action2 filter:
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.medicis.actions</param-value>
</init-param>
I have this property set on my struts.propertie file :
struts.action.extension=action
Still i'm unable to launch my action using localhost:8080/starter/user.action :
There is no Action mapped for namespace / and action name user
I really don't know what's going wrong, I even downloaded a simple example without any result either.
Could it be a configuration problem due to eclipse, m2eclipse, maven wtp or geronimo ???
If you need more information just tell me and i'll provide you that asap.
Create struts.properties in "src" folder
Copy and paste code from below:
struts.convention.package.locators = actions
struts.convention.action.suffix = Controller
struts.convention.action.mapAllMatches = true
Create class called IndexController in com.example.actions
Copy and paste code from below:
package com.example.actions;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import com.opensymphony.xwork2.ActionSupport;
public class UserController extends ActionSupport {
public String index(){
return SUCCESS
}
}
You also need to create jsp file in WEB-INF/content/user/index.jsp

Accessing second servlet when grails is configured in web.xml

How can you have a grails application as well as other servlets defined in your web.xml?
I want to have it so that some url patterns are handled by a servlet while all others are handled by Sitemesh/grails.
The default configuration of web.xml generated by grails is:
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>grailsWebRequest</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>urlMapping</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>grails</servlet-name>
<servlet-class>org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
I then added the configuration to web.xml for my 2nd servlet:
<servlet>
<servlet-name>Tracepoints</servlet-name>
<servlet-class>com.mydomain.Tracepoints</servlet-class>
<init-param>
<param-name>hostName</param-name>
<param-value>http://www.mydomain.com/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Tracepoints</servlet-name>
<url-pattern>*.tpoints</url-pattern>
</servlet-mapping>
But the above doesn't allow me to access my non grails servlet (with the url: domain.com/hello.tpoints) and trying it gets me a 404. I do know that the servlet's class files are deployed with the war because they exist in WEB-INF/classes directory.
You need to give the Grails servlet a more specific url-pattern in the mapping, e.g. /grails/* or *.grails or so (you're free to choose), so that only the URL's matching those patterns invokes the Grails servlet.

Velocity + Struts2 + Sitemesh + Spring +Hibernate Integration How to configur web.xml?

I needed to create an application using Struts2 as MVC,Hibernate for data access and spring in the business logic.
And also I needed to use Velocity for presentaion and sitemesh for templating.
Integrating Hibernate and Spring is done easily but integrating spring, sitemesh and velocity together with Struts2
is not clear for me but I can use velocity,spring and sitemsh individually with Struts2.
Of course as illustrated in this example http://www.rkcole.com/articles/struts/crudTutorial/step4.html
sitemesh and spring can be integrated with struts2 configuring web.xml as
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Now my task is to integrate velocity with this combination...............
Normally to integrate Velocity and struts2 I use the following
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
.............................................................................................
Now my question is how to set `
<servlet-mapping>
`, its only for velocity, or simemesh or has to be set differently
Please let me know how to proceed,if can please reply with complete web.xml and others steps to be followed.
Regards
T.Thamilvaanan
Ya, finally I got this web.xml after lots of reading and searching............
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- A part in Spring Integration-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
<!-- All the filters starts here-->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.StrutsPrepareFilter</filter-class>
</filter>
<!-- This is used to integrate sitemesh with Struts2-->
<!--
I am using Velocity to create sitemesh decorators so I have to use
VelocityPageFilter to integrate
Sitemesh (i.e. Sitemesh in velocity) + Struts2
In the web.xml, the VelocityPageFilter should be placed between the
ActionContextCleanUp (StrutsPrepareFilter since 2.1.3 ) and
and the FilterDispatcher (StrutsExecuteFilter since 2.1.3)
-->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.apache.struts2.sitemesh.VelocityPageFilter</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring Integration-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Finally since I am velocity pages in struts2 MVC I am using
VelocityViewServlet to Integrate struts2 with Velocity -->
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.VelocityViewServlet
</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/tools.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
</welcome-file-list>
</web-app>
Hope this is fine,will test and let you know.
Cheers............
Regards
Thamilvaanan

Resources