spring-mvc mail not working with the ZK framework

user1306192

I have a problem replicating the following example on my site created with the ZK-framework: http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc

When submitting the form, I get:

HTTP ERROR 404

Problem accessing /backend/sendEmail.do. Reason:

Not Found

even though I've specified in my my web.xml to map .do files to the dispatcherservlet.

The relevant entries in web.xml are:

 <servlet>
    <servlet-name>SpringController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mail.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>SpringController</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

In pom.xml:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>compile</scope>
</dependency>

In spring-mail.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="mailSender"      class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="username" />
    <property name="password" value="password" />

    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/" />
    <property name="suffix" value=".zul" />
</bean>

<bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">Error</prop>
        </props>
    </property>
</bean>

The form .zul:

<zk xmlns:h="native">
    <window title="Recupera password">
        <h:form method="post" action="sendEmail.do">
            <grid hflex=" 1">
                <columns>
                    <column align="right" hflex="min" />
                    <column />
                </columns>
                <rows>
                    <row>
                        E-mail :
                        <hlayout>
                            <textbox id="email" cols="24" tabindex="1" />
                        </hlayout>
                    </row>
                    <row>
                        <label />
                        <button id="invia" type="submit" label="Invia" />
                    </row>
                </rows>
            </grid>
        </h:form>
    </window>
</zk>

and finally, the controller:

@Controller
@RequestMapping("/backend/sendEmail.do")
public class RecuperaPasswordController {

    @Autowired
    private JavaMailSender mailSender;

    @RequestMapping(method = RequestMethod.POST)
    public void invia(HttpServletRequest request) throws MessagingException {
    }

}

In theory everything should be in place, as far as I see, but it still gives me this error. Judging from similar posts, the problem might be that the .zul is not in the WEB-INF folder, but as far as I've understood, that folder is inaccessible, and for good reasons, since it might contain configuration files with sensitive information, so I'm kind of lost.

Otherwise, if someone knows a good way to integrate maildispatching with ZK, I'm all ears for other alternative solutions!

EDIT: As requested, the whole web.xml:

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

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <description><![CDATA[MyEfm]]></description>
    <display-name>MyEfm</display-name>

    <!-- //// -->
    <!-- ZK -->
    <listener>
        <description>ZK listener for session cleanup</description>
        <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
    </listener>
    <servlet>
        <description>ZK loader for ZUML pages</description>
        <servlet-name>zkLoader</servlet-name>
        <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>

        <!-- Must. Specifies URI of the update engine (DHtmlUpdateServlet). It 
            must be the same as <url-pattern> for the update engine. -->
        <init-param>
            <param-name>update-uri</param-name>
            <param-value>/zkau</param-value>
        </init-param>
        <!-- Optional. Specifies whether to compress the output of the ZK loader. 
            It speeds up the transmission over slow Internet. However, if you configure 
            a filter to post-processing the output, you might have to disable it. Default: 
            true <init-param> <param-name>compress</param-name> <param-value>true</param-value> 
            </init-param> -->
        <!-- [Optional] Specifies the default log level: OFF, ERROR, WARNING, INFO, 
            DEBUG and FINER. If not specified, the system default is used. <init-param> 
            <param-name>log-level</param-name> <param-value>OFF</param-value> </init-param> -->
        <load-on-startup>1</load-on-startup><!-- Must -->
    </servlet>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zul</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zhtml</url-pattern>
    </servlet-mapping>
    <!-- [Optional] Uncomment it if you want to use richlets. <servlet-mapping> 
        <servlet-name>zkLoader</servlet-name> <url-pattern>/zk/*</url-pattern> </servlet-mapping> -->
    <servlet>
        <description>The asynchronous update engine for ZK</description>
        <servlet-name>auEngine</servlet-name>
        <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>

        <!-- [Optional] Specifies whether to compress the output of the ZK loader. 
            It speeds up the transmission over slow Internet. However, if your server 
            will do the compression, you might have to disable it. Default: true <init-param> 
            <param-name>compress</param-name> <param-value>true</param-value> </init-param> -->
        <!-- [Optional] Specifies the AU extension for particular prefix. <init-param> 
            <param-name>extension0</param-name> <param-value>/upload=com.my.MyUploader</param-value> 
            </init-param> -->
    </servlet>
    <servlet-mapping>
        <servlet-name>auEngine</servlet-name>
        <url-pattern>/zkau/*</url-pattern>
    </servlet-mapping>

    <!-- [Optional] Uncomment if you want to use the ZK filter to post process 
        the HTML output generated by other technology, such as JSP and velocity. 
        <filter> <filter-name>zkFilter</filter-name> <filter-class>org.zkoss.zk.ui.http.DHtmlLayoutFilter</filter-class> 
        <init-param> <param-name>extension</param-name> <param-value>html</param-value> 
        </init-param> <init-param> <param-name>compress</param-name> <param-value>true</param-value> 
        </init-param> </filter> <filter-mapping> <filter-name>zkFilter</filter-name> 
        <url-pattern>your URI pattern</url-pattern> </filter-mapping> -->
    <!-- //// -->

    <!-- ///////////// -->
    <!-- DSP (optional) Uncomment it if you want to use DSP However, it is turned 
        on since zksandbox uses DSP to generate CSS. -->
    <servlet>
        <servlet-name>dspLoader</servlet-name>
        <servlet-class>org.zkoss.web.servlet.dsp.InterpreterServlet</servlet-class>
        <init-param>
            <param-name>class-resource</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dspLoader</servlet-name>
        <url-pattern>*.dsp</url-pattern>
    </servlet-mapping>

     <servlet>
        <servlet-name>SpringController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mail.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

<!--     <servlet-mapping> -->
<!--         <servlet-name>SpringController</servlet-name> -->
<!--         <url-pattern>*.zul</url-pattern> -->
<!--     </servlet-mapping> -->

    <servlet-mapping>
        <servlet-name>SpringController</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- Less compiler ZK Less Servlet da commentare in produzione / collaudo -->

    <!-- <servlet> -->
    <!-- <servlet-name>zkLess</servlet-name> -->
    <!-- <servlet-class>org.zkoss.less.ZKLessServlet</servlet-class> -->
    <!-- <init-param> -->
    <!-- <param-name>org.zkoss.less.LessResource</param-name> -->
    <!-- specify to the folder that contains *.less -->
    <!-- <param-value>/common/less</param-value> -->
    <!-- </init-param> -->
    <!-- <init-param> -->
    <!-- <param-name>org.zkoss.less.OutputFormat</param-name> -->
    <!-- specify output file suffix, default .css.dsp -->
    <!-- <param-value>.css.dsp</param-value> -->
    <!-- </init-param> -->
    <!-- <init-param> -->
    <!-- <param-name>org.zkoss.less.CompressOutput</param-name> -->
    <!-- compress output, default true -->
    <!-- <param-value>true</param-value> -->
    <!-- </init-param> -->
    <!-- <load-on-startup>1</load-on-startup> -->
    <!-- </servlet> -->
    <!-- <servlet-mapping> -->
    <!-- <servlet-name>zkLess</servlet-name> -->
    <!-- specify to folder that contains *.less -->
    <!-- <url-pattern>/common/less/*</url-pattern> -->
    <!-- </servlet-mapping> -->

    <!-- End Less compiler -->

    <!-- /////////// -->
    <!-- [Optional] Session timeout -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>


    <!-- Spring configuration -->
    <!-- Initialize spring context -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext.xml
        /WEB-INF/applicationContext-security.xml
    </param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>root-pf</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <filter><!-- the filter-name must be preserved, do not change it! -->
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <filter>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




    <!-- [Optional] MIME mapping -->
    <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/vnd.ms-word</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>gif</extension>
        <mime-type>image/gif</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>htm</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpeg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>txt</extension>
        <mime-type>text/plain</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xls</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xml</extension>
        <mime-type>text/xml</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>zhtml</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>zul</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>

    <welcome-file-list>
        <welcome-file>index.zul</welcome-file>
        <welcome-file>index.zhtml</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>
user1306192

I found the problem! It turns out that I had forgotten the context:component-scan in the spring-mail.xml bean configuration file as well as having the wrong header. As written here, this is necessary to find the controller with the requestmapping annotion. It was because I was earlier following a similar tutorial, that didn't include spring-mvc.

The correct spring-mail.xml (in my case) should start with:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="it.infosons.controller" />

Doing research I also found that many recommended to add the

@EnableWebMvc

annotation to the controller, but I found that this was not really necessary. Thanks to AVC, for his assistance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related