struts.xml
<action name="tohome" class="action.ToSmsAction" method="execute">
<result name="success">/pages/home.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
TosmsAction.java
public class ToSmsAction extends ActionSupport {
private String time;
public String execute() {
System.out.println(" time is " + getTime());
return SUCCESS;
} else {
return LOGIN;
}
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
if i am not including class="action.ToSmsAction" then it is working good
but when including class="action.ToSmsAction" it is showing following error
HTTP Status 500 -
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling
this request.
exception
org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is
usually caused by using Struts tags without the associated filter. Struts tags are only
usable when the request has passed through its servlet filter, which initializes the
Struts dispatcher needed for this tag. - [unknown location]
root cause
The Struts dispatcher cannot be found. This is usually caused by using Struts tags
without the associated filter. Struts tags are only usable when the request has passed
through its servlet filter, which initializes the Struts dispatcher needed for this tag. -
[unknown location]
note The full stack traces of the exception and its root causes are available in the
GlassFish Server Open Source Edition 3.1.1 logs.
Related
I have a JSF web application where all the pages that reside under directory web needs to be protected from unautheticatd use i.e., user should be in session to accesss these pages. I am using filter to validate the session for these pages. These pages are accessed via url like : /contextRoot/web/download.xhtml or /contextRoot/web/sign/upload.xhtml. Whereas other pages that reside outside web directory or in some other directory need not to go pass through session validation filter. My filter is like:
#WebFilter(filterName = "AuthenticationFilter", urlPatterns={"/web/*"}, dispatcherTypes = {DispatcherType.REQUEST})
public class AuthenticationFilter implements Filter {
private static final boolean debug = true;
private FilterConfig filterConfig = null;
public AuthenticationFilter() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (debug) {
log("AuthenticationFilter:doFilter()");
}
HttpSession session = ((HttpServletRequest) request).getSession(false);
if (session == null || session.getAttribute("username") == null) {
System.out.println("Your Session Not Active. You are redirected.");
//((HttpServletResponse) response).sendRedirect("home.xhtml");
} else {
System.out.println("Your Session is active. username : " + session.getAttribute("username"));
}
Throwable problem = null;
try {
chain.doFilter(request, response);
} catch (Throwable t) {
// If an exception is thrown somewhere down the filter chain,
// we still want to execute our after processing, and then
// rethrow the problem after that.
problem = t;
t.printStackTrace();
}
}
}
I am using urlPattern /web/* so that every page inside web directory will go pass this filter. The filter is right now just printing stuff for debugging. But whenever I am accessing page inside web directory or any other page, it is not going through filter. I also tried using /faces/web/* as urlPattern but that also didn't work. But when I put /* as urlPattern, every accessed page goes through the filter.
I am accessing page as
http://localhost:8080/CodesignWebApp/faces/web/sign/SelectServer.xhtml http://localhost:8080/CodesignWebApp/faces/web/sign/FileUpload.xhtml?signmethod=MICROSOFT
I am suspecting something wrong with urlPattern.
I am accessing page as
http://localhost:8080/CodesignWebApp/faces/web/sign/SelectServer.xhtml
http://localhost:8080/CodesignWebApp/faces/web/sign/FileUpload.xhtml
The URL pattern of a #WebFilter (and #WebServlet) must match exactly those URLs you see in browser's address bar (and thus not the disk file system paths you actually have in the server side; it's also literally called an "URL pattern", not "file pattern" or whatever).
So, all in all, just this should do, provided that /CodesignWebApp is webapp's context root:
#WebFilter("/faces/web/*")
public class AuthenticationFilter implements Filter {
// ...
}
(filter name is not relevant and request dispatcher method you specified is the default already)
A different alternative is to get rid of ancient JSF 1.0 style /faces/* mapping altogether and replace it by the JSF 2.0 style *.xhtml mapping. You don't want the endusers to see raw JSF source code when they remove /faces part from the URL, right?
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
This way you can just access the pages as follows:
http://localhost:8080/CodesignWebApp/web/sign/SelectServer.xhtml
http://localhost:8080/CodesignWebApp/web/sign/FileUpload.xhtml
And map the filter as follows:
#WebFilter("/web/*")
public class AuthenticationFilter implements Filter {
// ...
}
See also:
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
I'm trying to find a way to configure in struts.xml an error message for each type of exception that can be thrown by an Action class. In an action class I could accomplish something similar by catching an exception, calling addActionError(String), and rethrowing the exception (provided an <exception-mapping> exists). Is there a way to do this through configuration?
As a reference point, this functionality exists in Struts1 with the key attribute on an exception handler - I'm hoping to be able to do something similar.
<exception key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
In strut2 also you can define exception mappings. Refer http://struts.apache.org/release/2.1.x/docs/exception-configuration.html. You can have a common error.jsp which displays a message that is looked up based on the class name of the exception.
In Struts2 you can use the following mapping to pass on the key/message to result (result can be a jsp or another action class).
<global-exception-mappings>
<exception-mapping exception="com.test.exception.MyCustomException" result="error">
<param name="param">display.custom.error</param>
</exception-mapping>
</global-exception-mappings>
<global-results>
<result name="error" type="chain">handleAction</result>
</global-results>
<action name="handleAction" class="HandleExceptionAction">
<result name="result">/WEB-INF/jsp/error.jsp</result>
</action>
If it an action class, in case of chaining (if the user wants to process the exception) then you need to have a corresponding attribute in Action class with getters and setters.
public class HandleExceptionAction extends ActionSupport implements
ServletRequestAware, SessionAware {
private **String param**;
private HttpServletRequest httpRequest;
private static final Log LOG = LogFactory.getLog(InputAction.class);
public String execute(){
LOG.debug("inside excute().....");
LOG.debug("Parameter passed:" + param);
System.out.println("Parameter passed:" + param);
return "result";
}
I am using spring injection, hope it will hep you.
I have a question with respect to interceptors in Struts2
Struts2 provides very powerful mechanism of controlling a request using Interceptors. Interceptors are responsible for most of the request processing. They are invoked by the controller before and after invoking action, thus they sits between the controller and action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.
I have taken this above lines from:
http://viralpatel.net/blogs/2009/12/struts2-interceptors-tutorial-with-example.html
In this example you will see how the interceptors are invoked both before and after the execution of the action and how the results are rendered back to the user.
I have taken this above lines from
http://www.vaannila.com/struts-2/struts-2-example/struts-2-interceptors-example-1.html
I have written a basic interceptor and plugged it to my Action class:
public class InterceptorAction implements Interceptor {
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Action class has been called : ");
return success;
}
}
struts.xml
<action name="login" class="com.DBAction">
<interceptor-ref name="mine"></interceptor-ref>
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
As per the above statements from their sites , i assumed that this line Action class has been called would be two times on the console (That is before the Action and after the Action class ) , but it has been printed only once?
PLease let me know , if my understanding is wrong , or the authors were wrong in that site ??
Not taking the time to read the page lets clear a few things up...
You are missing an important step in your interceptor.
Struts2, uses an object called ActionInvocation to manage calling the interceptors.
Lets give ActionInvocation a name (invocation) and show how the framework starts the ball rolling:
ActionInvocation invocation;
invocation.invoke(); //this is what the framework does... this method then calls the first interceptor in the chain.
Now we know interceptors can do pre-processing and post-processing... but the interface only defines one method to do the work of the interceptor (init and delete are just life cycle)! If the interface defined a doBefore and doAfter it would be easy so there must be some magic, happening...
As it turns out you are responsible for giving control back to action invocation at some point in your interceptor. This is a requirement. If you don't give control back to the ActionInvocation you will break the chain.
So when creating an interceptor you do the following steps
Create a class which implements com.opensymphony.xwork2.interceptor.Interceptor
[optional] do pre-processing work
call ActionInvocations invoke method to carry on processing down the stack and capture the return value.
[optional] do post processing as the above call unwinds.
return the string from step 3 (the result string) unless you have reason to do otherwise.
And here is a complete but pretty useless example:
package com.quaternion.interceptors;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implements Interceptor{
#Override
public void destroy() {
//nothing to do
}
#Override
public void init() {
//nothing to do
}
#Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Something to do before the action!");
String resultString = invocation.invoke();
System.out.println("Something to do after the action!");
return resultString;
//if you are not doing post processing it is easiest to write
//return invocation.invoke();
}
}
I got an interceptor that I'm trying to get to output a stream when a certain action is calling. This is part of my code in the inteceptor:
InputStream inputStream;
public String intercept(ActionInvocation invocation) throws Exception
{
if (currAction.contentEquals("actionToTest"))
{
String result = "TRUE";
inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"));
return "resultToGiveStream";
}
}
inputStream has got it's own getters and setters.
And in struts.xml:
<global-results>
<result type="stream" name="resultToGiveStream">
<param name="contentType">text/plain</param>
<param name="inputName">inputStream</param>
</result>
</global-results>
But when I call actionToTest I only receive this in my console:
2011-maj-18 11:19:16 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
ALLVARLIG: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
Is it a lost cause to get it to output what I want? I haven' found anyone doing anything similar.
This code is an atempt for a workaround for my other question.
Struts2 is looking for the getInputStream() method on your action and it isn't finding it.
You could try placing the inputStream on the stack manually from within the interceptor. Something like:
invocation.getInvocationContext().put("inputStream", inputStream);
We are working on SmartGWT 2.2, and Struts2.
I have created a sample form(DynamicForm) which asks to upload file and mapped action class for processing file upload.
I have setup form.setCanSubmit(true);
My call is succefully gets transferred to struts action class and file is getting uploaded also.
struts.xml
<action name="FileUploadAction" class="FileUploadAction" >
<result name="success" type="redirect">success</result>
</action>
But the problem is that control is not getting back to...
form.submit(new DSCallback(){
#Override
public void execute(DSResponse response, Object rawData,
DSRequest request) {
System.out.println("Response: " + response.getHttpResponseCode());
SC.say("back");
System.out.println("BACK...........");
}
});
I read in Smartgwt, Dynamic Form API that,
if this.canSubmit is true, callback is ignored..
As, we are not using DataSource, I have to use this.canSubmit to true.
Response from ActionClass gets struck at 'http://127.0.0.1:8888/success'
So, what is an alternate solution?
you can use hidden iframe and JSNI call that hidden iframe from your server action class....
Thank you.