ModelAndView无法正常工作

时间:2016-08-07 09:37:06

标签: java servlets model-view-controller modelandview

我已经用java编写了代码。所有事情都正常运作。我只想返回没有返回正确网页的modelandview的值。它扔空白页。我的控制器的代码是:

public class Part3 extends HttpServlet {

Num num = new Num();
String name[];
Record record = null;

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ModelAndView mv = new ModelAndView();
    String number = request.getParameter("number");
    num.setNumber((number));

    Driver driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("neo4j", "neo4j"));
    Session session = driver.session();
    StatementResult result = session.run("MATCH (tom {name: '" + number + "'}) RETURN tom.name, tom.born, tom.roles;");
    while (result.hasNext()) {
        record = result.next();
    }
    session.close();
    driver.close();

    request.setAttribute("selectObject", record);
    mv.setViewName("index");
    return mv;
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    try {
        handleRequest(request, response);
    } catch (IOException ex) {
        Logger.getLogger(Part3.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

我的HTML页面是index.html,其代码是:

<form action ="number" name ="number">
            <b>Enter Song ID:</b>
            <input type="text" name='number' required  placeholder="Enter SongID"><br /><br /><br />
            <input type="submit"  value="Submit" class="btn btn-primary" style="background-color: orangered"/> 
        </form>
        ${selectObject}
    </div>

1 个答案:

答案 0 :(得分:0)

ModelAndView是一个spring mvc对象。你不能在servlet中使用它。

你的servlet什么都不做,在你需要写入对象HttpServletResponse的servlet中。 您尝试使用此代码handleRequest(request, response);做什么?

更新:

首先,在对象响应中,您可以获得PrintWriter:response.getWriter()。在本作者中写下回应 其次,删除ModelAndView,你不需要它。在使用Spring MVC之前尝试理解什么是servlet。

以下是一个简单示例:http://www.tutorialspoint.com/servlets/servlets-first-example.htm