我正在尝试创建一个使用 sling servlet 处理的简单 html 表单。这是一个非常基本的表单,有几个输入字段,我的 java 类非常简单。我要做的就是在我的错误日志中记录表单中的值。相反,什么都没有发生。我一定是错过了一些小东西,或者只是完全失明。
表单html
<form name="" method="POST" action="/apps/form">
<input id="firstName" type="text" name="firstName" />
<input id="phoneNumber" type="text" name="phoneNumber" />
<input type="submit" name="submit" value="submit">
</form>
Java 类
@Component(immediate=true, metatype=false, label="FORM SERVLET")
@Properties({
@Property(name="sling.servlet.methods", value={"POST"}),
@Property(name="sling.servlet.paths", value={"/apps/form"}),
@Property(name="sling.servlet.selectors", value={"form"}),
@Property(name="sling.servlet.extensions", value={"html"})
})
public class FormServlet extends SlingSafeMethodsServlet {
private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("firstName");
String phone = request.getParameter("phoneNumber");
out.println(name);
out.println(phone);
log.error("the chat component is loaded first name" + name + phone);
}
}