简体   繁体   中英

problem with struts actions and redirections

I am trying to update a simple web app that was built with struts2, jsp and standard servlets. I am trying to redirect a url to a specific action but can't seem to get it to work right. For example, the url that is correct is:

http://localhost:8080/theapp/lookup/search.action

Here is my web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                     "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
<display-name>theapp</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>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

And here is my struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration    
2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<default-action-ref name="search" />

<action name="search" method="search" class="com.theapp.SearchAction" >
    <result>index.jsp</result>
    <result name="input" >index.jsp</result>
    <result name="error" type="redirect">site_locator_mobile/error.action</result>
</action>

The problem here is that if I don't specify the correct url as above, I just get the index.jsp file, but without any properties in index.jsp being processed because the information is contained in the servlet.

What I would like to is if someone just entered:

http://localhost:8080/theapp/lookup/ 

than they would be taken to:

http://localhost:8080/theapp/lookup/search.action

Thanks

Maybe you can specify a servlet as a welcome file:

<welcome-file-list>
<welcome-file>search.action<welcom-file>
<welcome-file-list> 

Note: some people report that a file called search.action must actually exist for this to work.

Another option is to have your index.jsp redirect to search.action and use another result jsp:

<% response.sendRedirect("search.action"); %>

Edit: It is also possible to add a filter-mapping directive directly to web.xml which redirects requests for /theapp/lookup/ to /theapp/lookup/search.action

Seems more complex, though.

This may sound stupid, but can you post the entire contents of struts.xml file? Have you added a namespace to the struts xml file. You are trying to look up myApp/lookup/someAction.action. I think you should have a name space defined for your package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM