简体   繁体   English

基本表单Portlet JSR 286 v Liferay 6.0

[英]Basic form portlet jsr 286 v liferay 6.0

How to retrieve data from a form and to send it to a MySQL database via JDBC? 如何从表单中检索数据并通过JDBC将其发送到MySQL数据库?

Here's my procedure: 这是我的程序:

main class: 主类:

public class Dati extends MVCPortlet {

    public static Connection con() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            System.out.println("Errore");
        }

        // APRI CONNESSIONE

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/db_alex","root","25071984");
            System.out.println("Connessione effettuata");
            return conn;
        } catch (SQLException ex) {
            System.out.println("SQlException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            return null;
        }
    }

    public void Invio (ActionRequest actionRequest, ActionResponse actionResponse) 
    throws PortletException, IOException {
        String username = actionRequest.getParameter("username");
        String password = actionRequest.getParameter("password");

        Connection conn = null;

        try {

        Statement st = conn.createStatement();
        st.executeUpdate("INSERT INTO contacts (username,password) values ('"+username+"','"+password+"')");
        System.out.println("Inserimento avvenuto con successo");
        } catch (Exception e) {
            System.out.println("Inserimento non avvenuto");
        }
    }
}

view.jsp view.jsp

<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<portlet:actionURL name="Invio" var="Invio"/>

<form method="post" action="actionURL/>">

    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Invia!">    

</form> 

I don't know where my fault is. 我不知道我的错在哪里。 I was reading Portlet is temporarily unavailable and applying that rule, but Liferay says I'm wrong. 我当时在阅读Portlet暂时不可用并应用该规则,但是Liferay说我错了。

Which is the correct procedure to store? 正确的存储程序是?

you define actionURL Invio as a java object in view.jsp but you reference it wrong. 您将actionURL Invio定义为view.jsp中的java对象,但是引用错误。 you should reference it in <%= %> tag. 您应该在<%=%>标记中引用它。 actually your form tag should look like this: 实际上,您的表单标签应如下所示:

<form method="post" action='<%=Invio%>'>

<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Invia!">    

Please check the action attribute's value of form tag. 请检查表单标签的动作属性值。

You can use in many ways. 您可以通过多种方式使用。

1.  <form method="post" action="<portlet:actionURL/>">
       <input type="text" name="username">
       <input type="password" name="password">
       <input type="submit" value="Invia!">    
   </form> 

2. 2。

    <form method="post" action="${Invio}">
       <input type="text" name="username">
       <input type="password" name="password">
       <input type="submit" value="Invia!">    
    </form> 

3. 3。

    <form method="post" action="<%=Invio%>">
       <input type="text" name="username">
       <input type="password" name="password">
       <input type="submit" value="Invia!">    
    </form> 

Third one I don't like. 我不喜欢的第三个人。

Choice is yours.... 选择是你的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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