如何在Adobe CQ 5.5中发布表单?

时间:2014-03-04 05:28:20

标签: cq5 jcr sling

我对CQ很新,我很长时间以来一直在努力解决这个问题。我想要做的是“使用常规标记创建一个接受用户名密码的页面,并将数据发布到servlet。

servlet使用硬编码的str.equals(“username”)检查用户名密码,并重定向到另一个页面,即成功或失败“ 请注意,我正在构建一个拥有普通用户的网站,例如“在stackoverflow等网站上注册的用户”这些用户非作者可以编辑内容。 一项非常基本的任务,但对我来说太难了。这是代码。 我使用CRXDE成功创建了捆绑邮件servlet

package com.example;
import java.io.IOException;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=true, metatype=false, label="EXAMPLE SERVLET")
@Service
@Properties(value = {   
        @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value={"POST"}),
        @org.apache.felix.scr.annotations.Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}),
        @org.apache.felix.scr.annotations.Property(name="sling.servlet.selectors", value={"SELECTORNAME"}),
        @org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"html"})
})
public class ExampleServlet extends SlingAllMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(ExampleServlet.class);
    private static final long serialVersionUID = 1L;

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        //String redirect = request.getParameter(":redirect");

        log.info("The Sling Post Servlet- Example Servlet has been called !! ");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username.equals("oliver")&& password.equals("oliver"))
        {
            response.sendRedirect("/content/mywebsite/en/products");
        }
        else
        {
            response.sendRedirect("/content/mywebsite/en/services");
        }
        log.info("Sucessfull Response Sent ");
    }

} 

I get error as 
Status  
200
Message 
OK
Location    /example.SELECTORNAME.html
Parent Location /
Path    
/example.SELECTORNAME.html
Referer http://localhost:4502/content/mywebsite/en/products.html
ChangeLog   
<pre>modified("/example.SELECTORNAME.html/username");<br/
  

和jsp如下

<%--

  My Content Page Componenet component.

  General Description

--%><%
%><%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" %>

<%
%><%
    // TODO add you code here
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<cq:include script="head.jsp"/>



<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My title</title>
</head>
<body>

<div>My First Page </div>
<form action="/example.SELECTORNAME.html" method="POST">
<input type="text" name ="username"/>
<input type="password" name "password"/>
<input type="submit" value="Login"/>

</form> 

</body>
</html>

提前致谢!!

1 个答案:

答案 0 :(得分:2)

文件中缺少Servlet路径。 / **  * @scr.component metatype =“false”  * @scr.service interface =“javax.servlet.Servlet”  * @scr.property name =“sling.servlet.paths”values =“/ bin / login”  * / 公共类LoginServlet扩展SlingAllMethodsServlet { ...... }

jsp -

form name =“frmLogin”id =“frmLogin”method =“post”action =“/ bin / login”