I am using struts2 for web development.
after deploying
it is showing url http://localhost:8084/iland/ which is login page of my site
which is correct but after login it is redirected to 'about' action which fetch user details so in this case url should be http://localhost:8084/iland/about,
but it is showing url http://localhost:8084/iland/pages/about.
Here 'pages' is included, due to this change my css are not get loaded.
How to resolve this
my struts.xml for above is
<package name="myprofile" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success" type="redirect">about</result>
<result name="input">/pages/login.jsp</result>
</action>
<package name="default" extends="struts-default">
<action name="about" class="social.action.fetchBasicProfile">
<result name="success">/pages/profile/about.jsp</result>
<result name="input">/pages/profile/about.jsp</result>
<result name="login">/pages/pleaselogin.jsp</result>
</action>
</package
There must be problem with your namespace, so please check your struts.xml and change the namespace="pages" to namespace="/". this will resolve your issue. let me know if u find any difficulty
Related
I have different areas in my webapp, basically:
Login actions. No special interceptor needs.
Secure zone. Only for authenticated users. AuthenticationInterceptor redirects non authenticated users to logout action.
Admin zone. Only for admin users. AdministrationInterceptor redirects non admin users to logout action.
Hence, I have divided my actions in packages.
numeritos-default is a package with no actions, just to define the
interceptors and a couple stacks, in order to inherit from it.
numeritos-login is a package that inherits directly from
struts-default and, hence, uses the defaultStack from struts.
numeritos-secure inherits from numeritos-default and SHOULD (afaik)
use the numeritos-default default stack.
numeritos-admin inherits
from numeritos-default but defines a different default stack, in
order to add the administrationInterceptor to the default stack.
The thing is, the actions in numeritos-secure are executing the administrationInterceptor when they shouldn't, since their default stack, inherited from numeritos-default doesn't include that interceptor (I can't figure out the reason), hence only admin users can access the application. The rest of users are kicked out of the whole secure zone, when they should be only kicked out of the admin zone.
I have the following struts.xml configuration file. Sorry to post the whole of it, but I think it could help. All classes references are really Spring beans names, since all dependencies are managed by Spring.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="global-messages" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="numeritos-default" extends="struts-default">
<interceptors>
<!-- Session authentication interceptor -->
<interceptor name="authenticationInterceptor" class="authenticationInterceptor"/>
<!-- Administration privileges interceptor -->
<interceptor name="administrationInterceptor" class="administrationInterceptor"/>
<!-- Caching headers interceptor -->
<interceptor name="cachingHeadersInterceptor" class="cachingHeadersInterceptor"/>
<!-- Default interceptor stack for this package (default + authentication) -->
<interceptor-stack name="numeritosDefaultStack">
<interceptor-ref name="authenticationInterceptor"/>
<interceptor-ref name="cachingHeadersInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Default interceptor stack for administration packages (default + administration) -->
<interceptor-stack name="numeritosAdminStack">
<interceptor-ref name="administrationInterceptor"/>
<interceptor-ref name="numeritosDefaultStack"/>
</interceptor-stack>
</interceptors>
<!-- Secure interceptor stack as default -->
<default-interceptor-ref name="numeritosDefaultStack"/>
</package>
<package name="numeritos-login" extends="struts-default">
<action name="welcome" class="welcomeAction">
<result name="success" type="freemarker">ftl/login.ftl</result>
</action>
<action name="login" class="loginAction">
<result name="success" type="redirectAction">exerciseLoad</result>
<result name="input" type="freemarker">ftl/login.ftl</result>
</action>
</package>
<package name="numeritos-secure" extends="numeritos-default">
<!-- Common login result -->
<global-results>
<result name="login" type="redirectAction">logout</result>
</global-results>
<action name="exerciseLoad" class="exerciseLoadAction">
<result name="simple_op" type="freemarker">ftl/simple_operation_view.ftl</result>
<result name="equation" type="freemarker">ftl/equation_view.ftl</result>
</action>
<action name="exerciseCheck" class="exerciseCheckAction">
<result name="success" type="freemarker">ftl/show_exercise_result.ftl</result>
<result name="input" type="freemarker">ftl/answer_format_error.ftl</result>
</action>
<action name="stats" class="statsAction">
<result name="success" type="freemarker">ftl/stats.ftl</result>
</action>
<action name="modulesList" class="modulesListAction">
<result name="success" type="freemarker">ftl/modules_list.ftl</result>
</action>
<action name="selectModule" class="selectModuleAction">
<result name="input" type="freemarker">ftl/modules_list.ftl</result>
<result name="success" type="redirectAction">exerciseLoad</result>
</action>
<action name="userSettings" class="userSettingsAction">
<result name="success" type="freemarker">ftl/user_settings.ftl</result>
</action>
<action name="changePassword" class="changePasswordAction">
<result name="success" type="freemarker">ftl/user_settings.ftl</result>
<result name="input" type="freemarker">ftl/user_settings.ftl</result>
</action>
<action name="logout" class="logoutAction">
<result name="success" type="freemarker">ftl/login.ftl</result>
</action>
</package>
<package name="numeritos-admin" extends="numeritos-secure">
<!-- Admin interceptor stack as default -->
<default-interceptor-ref name="numeritosAdminStack"/>
<!-- Common login result -->
<global-results>
<result name="login" type="redirectAction">logout</result>
</global-results>
<action name="adminConsole" class="adminConsoleAction">
<result name="success" type="freemarker">ftl/admin_console_results.ftl</result>
</action>
<action name="adminConsoleGroups" class="adminConsoleGroupsAction">
<result name="success" type="freemarker">ftl/admin_console_groups.ftl</result>
</action>
<action name="adminCreateGroup" class="adminConsoleGroupsAction" method="create">
<result name="success" type="redirectAction">adminConsoleGroups</result>
</action>
<action name="adminDeleteGroup" class="adminConsoleGroupsAction" method="delete">
<result name="success" type="redirectAction">adminConsoleGroups</result>
</action>
</package>
<package name="json-admin" extends="numeritos-default, json-default">
<interceptors>
<!-- Default interceptor stack for this package (default + administration + json) -->
<interceptor-stack name="numeritosJsonAdminStack">
<interceptor-ref name="administrationInterceptor"/>
<interceptor-ref name="json"/>
<interceptor-ref name="numeritosDefaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="numeritosJsonAdminStack"/>
<action name="jsonTest" class="jsonTestAction">
<result name="success" type="json">
<param name="root">users</param>
</result>
</action>
</package>
</struts>
Defining package configuration elements are only for adding additional configuration to packages that extend it. The packages can contain interceptors, interceptor stacks, actions that can be extended.
The package element has one required attribute name, which acts as the key for later reference to the package. The extends attribute is optional and allows one package to inherit the configuration of one or more previous packages
including all interceptor, interceptor-stack, and action configurations.
Usually packages are separated by namespace attribute which is essential for configuring actions. You can define the abstract package which doesn't contain the action configurations.
The optional abstract attribute creates a base package that can omit the action configuration.
The thing is, you are not using namespace, nor abstract packages and default-intercepto-ref is not inherited to the packages that extends it.
we are converting our application from Struts 1.2 to 2.16. I ahve added the requried jars and now i am converting struts_config.xml to struts.xml. Below is i struts-config.xml
<struts-config>
<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward name="welcome" path="/Welcome.do"/>
<forward name="family" path="/Employee.do"/>
<forward name="line" path="/Owner.do"/>
</global-forwards>
<action-mappings>
<action path="/Welcome" forward="/pages/welcome.jsp"/>
<action path="/Employee" forward="/pages/employeejsp"/>
<action path="/Owner" forward="/pages/owner.jsp"/>
</action-mappings>
</stuts-config>
I am not able convert Global_forwards to global_results. when trying to access the page getting a blank page and its not throwing any error. Did add devmode still no luck.
Could any one please help me .
Thanks
<forward name="welcome" path="/Welcome.do"/>
<forward name="family" path="/Employee.do"/>
<forward name="line" path="/Owner.do"/>
</global-forwards>
to
<global-results>
<result name="family" type="redirect">Employee</result>
<result name="line" type="redirect">Owner</result>
</global-results>
for action mapping
<action-mappings>
<action path="/Welcome" forward="/pages/welcome.jsp"/>
</action-mappings>
<package name="test" extends="struts-default">
<action name="Welcome" class="org.xyz.Welcome" method="welcome">
<result name="success">/welcome.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
im in learning struts 2.
i create a simple project that can add and list the PRODUCT that user add. list is my first page and show all product that added. it is my struts.xml file:
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="listProduct" />
<action name="listProduct" class="control.ProductHandler" method="list">
<result name="success">/list.jsp</result>
</action>
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success">/listProduct</result>
</action>
<action name="*Form">
<result>{1}.jsp</result>
</action>
</package>
</struts>
when i enter localhost:8080/product in then browser it show me list.jsp file. in this file i have a link that with it i can goto add.jsp file. href of this link is addForm.
in add.jsp file i have 3 text field and a submit button. when entered data added action class return "success" and i most goto localhost:8080/product (default page) but it show me :
HTTP Status 404 - /product/listProduct
this is my action file:
public String list(){
products=Database.get();
return "success";
}
public String add(){
if (add!=null){
Product product=new Product(name, producer, price);
Database.add(product);
}
return "success";
}
where is my mistake?
thanks.
In order to redirect to another action use redirectAction result type.
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success" type="redirectAction">listProduct</result>
</action>
In my struts.xml I have the following configured in order to handle redirection of myhost.com/admin and myhost.com/admin/ correctly:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<action name="admin">
<result type="redirectAction">admin/home</result>
</action>
<action name="admin/">
<result type="redirectAction">admin/home</result>
</action>
Is there a quicker way to do this in Struts2?
You are mixing two things: action and namespace. You can use the below config:
<action name="admin">
<result type="redirectAction">admin/</result>
</action>
<action name="" namespace="admin">
<result>/admin/index.jsp</result>
</action>
AS commented by yourself, have you tried the wildcard like this
admin/**
hope this helps
Hi We are developing a Struts2Portlet Application in WebSpherePortal6.0.1. In my application I want to redirect to another portlet page after fullfilling the validations in my struts action class.How to achieve it. Please help me.
Thanks in Advance.
I have done some R&D to fix this issue,finally i found a solution.We cant call/redirect the portal page from struts2Portlet Action class.We can have to give a result type in strut action class,then we have to configure the action in the result.
<action name="view" class="com.ibm.rock.ViewAction" method="prepareview">
<result name="view">*/view/viewportalpage.action*</result>
</action>
<action name="viewportalpage" class="com.ibm.rock.ViewAction" method="prepareview">
<result name="preview">/_Rock/jsp/html/Preview.jsp</result>
</action>
Synatax:
<result name="success">*/Namespace/view.action*</result>
<action name="view" class="com.ibm.rock.ViewAction" method="prepareview">
<result name="preview">/_Rock/jsp/html/Preview.jsp</result>
</action>