【问题标题】:WebApp using JSP and Java使用 JSP 和 Java 的 WebApp
【发布时间】:2018-08-20 16:00:19
【问题描述】:

我是 JSP 和 WebApplications 的新手,遇到了一个问题。我查看了很多文章,但没有设法使用 Java 从文件中加载数据并将其显示在网页上。

我在主函数中读取数据并用JSP显示。

这是java类的代码:

package org.mypackage.hello;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class NameHandler {
private String name;
private String type;
private String assignmentName;
private String moduleCode;
private String moduleName;
private String weight;
private String date;


public NameHandler() {
    name = name;
    type = null;
    assignmentName = null;
    moduleCode = null;
    moduleName = null;
    weight = null;
    date = null;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getAssignmentName() {
    return assignmentName;
}

public void setAssignmentName(String assignmentName) {
    this.assignmentName = assignmentName;
}

public String getModuleCode() {
    return moduleCode;
}

public void setModuleCode(String moduleCode) {
    this.moduleCode = moduleCode;
}

public String getModuleName() {
    return moduleName;
}

public void setModuleName(String moduleName) {
    this.moduleName = moduleName;
}

public String getWeight() {
    return weight;
}

public void setWeight(String weight) {
    this.weight = weight;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}




public static void main(String[] args) {

    NameHandler obj = new NameHandler();

    try (Scanner read = new Scanner (new File("file.txt"))) {
        read.useDelimiter(",");


        while(read.hasNext()){
            obj.setType(read.next());
            obj.setAssignmentName(read.next());
            obj.setModuleCode(read.next());
            obj.setModuleName(read.next());
            obj.setWeight(read.next());
            obj.setDate(read.next());


            System.out.println(obj.getType());
            System.out.println(obj.getAssignmentName());
            System.out.println(obj.getModuleCode());
            System.out.println(obj.getModuleName());
            System.out.println(obj.getWeight());
            System.out.println(obj.getDate());
            System.out.println("-------------------------");



        }
        read.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(NameHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}

这里是索引html文件:

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <h1>Entry Form</h1>

    <form name="Name Input Form" action="response.jsp">
        Enter your name:
        <input type="text" name="name" />
        <input type="submit" value="OK" />
    </form>

</body>

这里是jsp文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <jsp:useBean id="mybean" scope="session"     class="org.mypackage.hello.NameHandler" />
    <jsp:setProperty name="mybean" property="name" />
    <jsp:getProperty name="mybean" property="name" />


    <jsp:useBean id="stringBean" class="org.mypackage.hello.NameHandler" />


    <jsp:setProperty name="stringBean" property="assignmentName"  value="propertyValue" />

    <ol>
        <li><jsp:getProperty name="stringBean"   property="assignmentName" /></li>
        <li><jsp:getProperty name="stringBean" property="type" /></li>
        <li><jsp:getProperty name="stringBean" property="moduleCode" /></li>
        <li><jsp:getProperty name="stringBean" property="moduleName" /></li>
        <li><jsp:getProperty name="stringBean" property="weight" /></li>
        <li><jsp:getProperty name="stringBean" property="date" /></li>
    </ol>
 </body>

 </html>

我看到您可以将 JSP 文件与 java 类链接,但我真的不知道是否可以将它与主要内容链接。它继续打印 null。

【问题讨论】:

    标签: java jsp web-applications


    【解决方案1】:

    您在main方法中输入了设置值的代码,没有执行。例如,将其移动到 NameHandler 构造函数。 但是,设置值的正确位置是在另一个类中。

    【讨论】:

    • 我确实这样做了.. 但现在我收到有关 的错误。跨度>
    • 你能发布你收到的错误信息吗?尝试只执行页面的一部分。在语句&lt;jsp:setProperty name="mybean" property="name" /&gt; 中,您为属性赋值,但值是什么?试试,例如:&lt;jsp:useBean .../&gt; &lt;jsp:setProperty name="mybean" property="name" **value="aName"**/&gt; &lt;jsp:getProperty name="mybean" property="name" /&gt;
    猜你喜欢
    • 2012-11-06
    • 2013-08-15
    • 2011-09-27
    • 1970-01-01
    • 2015-06-30
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    相关资源
    最近更新 更多