1

我的 spring MVC 控制器不断收到 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.NullPointerException

我的代码如下:

电影.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@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>
    <c:choose>
    <c:when test="true">
        <h2>Please enter the details below:</h2>
        <form:form method="POST" commandName="movie">
            <table>
                <tr><td>Lead Actor:</td><td><form:input path="actor"/></td></tr>
                <tr><td>Lead Actress:</td><td><form:input path="actoress"/></td></tr>
                <tr><td>Genre:</td><td><form:input path="genre"/></td></tr>
                <tr><td>Year:</td><td><form:input path="year"/></td></tr>
            </table>
                <form:hidden path="way" value="add"/>
                <form:hidden path="page" value="5"/>
                <input type="submit" value="Add Moives"/>
        </form:form>

     </c:when>
     <c:when test="${requestScope.page eq '2'}">
        <h2>Searching Movies</h2>
        <form:form method="POST" commandName="movie">
            Keyword:<form:input path="key"/><br/>
            <form:radiobutton path="search" value="title"/>Search By Title<br/>
            <form:radiobutton path="search" value="actor"/>Search By Actor<br/>
            <form:radiobutton path="search" value="actress"/>Search By Actress<br/>
            <form:hidden path="way" value="browse"/>
            <form:hidden path="page" value="5"/>
            <input type="submit" value="Search Moives"/>
        </form:form>
    </c:when>
    <%-- <c:when test="${requestScope.page eq '3'}">
        <p>Add movie successful</p>
     </c:when>
    <c:when test="${requestScope.page eq '3'}">
        <p>Movie Title:${requestScope.title}</p>
        <p>Actor:${requestScope.actor}</p>
        <p>Actress:${requestScope.actress}</p>
        <p>Genre:${requestScope.Genre}</p>
        <p>Year:${requestScope.Year}</p>
    </c:when> --%>
    <c:otherwise>
        <h2>Welcome to our Movie Store</h2>
        <p>Please make your selection below</p>
        <!--<form method="post" action="movie.htm">
            <select name="page">
                <option value="2">Browse Movies</option>
                <option value="1">Add New Movie to Database</option>
            </select>
            <input type="submit" value="Send"/>
        </form> -->
        <form:form method="POST" action="movie.htm" commandName="movie">
            <form:select path="page">
                <form:option value="1">Add New Movie to Database</form:option>
                <form:option value="2">Browse Movies</form:option>
            </form:select>
            <input type="submit" value="Send"/>
        </form:form>
    </c:otherwise>
    </c:choose>
</body>
</html>

这是我的控制器:

public class movieController extends SimpleFormController {

 public movieController() {
     //Initialize controller properties here or 
    //in the Web Application Context

    setCommandClass(Movie.class);
    setCommandName("movie");
    setSuccessView("movie");
    setFormView("movie");
 }

/*
@Override
protected void doSubmitAction(Object command) throws Exception {
    throw new UnsupportedOperationException("Not yet implemented");
}
* */
//Use onSubmit instead of doSubmitAction 
//when you need access to the Request, Response, or BindException objects


 @Override
 protected ModelAndView onSubmit(
 HttpServletRequest request, 
 HttpServletResponse response, 
 Object command, 
 BindException errors) throws Exception {
 //Do something...
 Movie movie = new Movie();
 String page = movie.getPage();
 if(page.equals("1")||page.equals("2")){
     request.setAttribute("page",'1');
 }
 else{
    Connection con = null;
Statement statement = null;
ResultSet st = null;
    if(movie.getWay().equals("add")){
        String title = movie.getTitle();
        title = title.replaceAll("[^A-Za-z0-9\\s]","");
        String actor = movie.getActor();
        actor = actor.replaceAll("[^A-Za-z0-9\\s]","");
        String actress = movie.getActoress();
    actress = actress.replaceAll("[^A-Za-z0-9\\s]","");
        String genre = movie.getGenre();
        genre = genre.replaceAll("[^A-Za-z0-9\\s]","");
        String year_temp = movie.getYear();
        year_temp = year_temp.replaceAll("[^A-Za-z0-9\\s]","");
        int year = Integer.parseInt(year_temp);
        try{
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/moviedb",
      "root","");
                String sql = "INSERT INTO movies (title,actor,actress,genre,year) VALUES
      ('"+title+"','"+actor+"','"+actress+"','"+genre+"','"+year+"')";
                statement = con.createStatement();
                statement.executeUpdate(sql);
        } catch (SQLException ex) {
    // TODO Auto-generated catch block
    System.out.println("error happend:" + ex);
        } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    System.out.println("error happend:" + e);
        }
        finally{
    try{
        if(statement != null) statement.close();
        if(con != null) con.close();
                    //request.setAttribute("page", "3");
    } catch (SQLException ex) {
        System.out.println("error happend:" + ex);
    }
        }
    }
    else{
        String search = movie.getSearch();
        String key = movie.getKey();
        key = key.replaceAll("[^A-Za-z0-9\\s]","");
        try{
            Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/moviedb", "root","");
    String sql = "select * from movies where "+search+"='"+key+"'";
    statement = con.createStatement();
    st = statement.executeQuery(sql);
    while (st.next()) {
                request.setAttribute("page", "4");
                request.setAttribute("title",st.getString(1));
                request.setAttribute("actor",st.getString(2));
                request.setAttribute("actress",st.getString(3));
                request.setAttribute("Genre",st.getString(4));
                request.setAttribute("Year",st.getInt(5));
    }
        } catch (SQLException ex) {
    // TODO Auto-generated catch block
    System.out.println("error happend:" + ex);
        } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    System.out.println("error happend:" + e);
        }
        finally{
    try{
                if(st != null) st.close();
                if(statement != null) statement.close();
                if(con != null) con.close();                    
    } catch (SQLException ex) {
                System.out.println("error happend:" + ex);
    }
        }
    }
 //End
 }
 return new ModelAndView("movie","movie",movie);
 }
}

非常感谢!

4

1 回答 1

0

这些行看起来是您的 onSubmit() 中的问题:

Movie movie = new Movie();
String page = movie.getPage();

您正在创建一个新的 Movie 对象,而不是从 onSubmit() 参数转换和分配命令对象,不应该是:

Movie move = (Movie)command;
String page = movie.getPage();
于 2013-02-28T10:31:42.543 回答