2013-02-23 97 views
1

我的Spring MVC控制器不斷收到org.springframework.web.util.NestedServletException:請求處理失敗;嵌套的例外是顯示java.lang.NullPointerExceptionSpring MVC請求處理失敗

我的代碼如下:

movie.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); 
} 
} 

非常感謝!

+1

請分享完整的堆棧跟蹤 – 2013-02-23 05:39:25

回答

0

這些線看起來是在你的onsubmit()的問題:

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

您正在創建一個新的電影對象,而不是鑄造和來自的onsubmit()參數指定的命令對象,應該不會吧成爲:

Movie move = (Movie)command; 
String page = movie.getPage(); 
相關問題