Radim jednu web aplikaciju uz pomocu Spring MVC,
Odradio sam uz pomoc jednog tutoriala dosta toga, problem je kada pokrenem projekat javlja gresku u get metodi...
odnosno Controleru...
Moze li pomoc...
Dodao sam u postu kod...
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import java.sql.SQLException;
import javax.validation.Valid;
import model.Impression;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ImpressionController {
@RequestMapping(value="/impression", method = RequestMethod.GET)
public String createForm(ModelMap model) throws ClassNotFoundException, SQLException {
model.addAttribute("impression", new Impression());
model.addAttribute("impressions", Impression.allImpresion());
return "impresion";
}
@RequestMapping(value="/impresion", method = RequestMethod.POST)
public String allImpresion(@ModelAttribute("impresion") @Valid Impression impression, ModelMap model) throws ClassNotFoundException, SQLException {
impression.insertImpresion();
createForm(model);
return "impresion";
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Impression {
private int id;
private String username;
private String text;
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getText() {
return text;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setText(String text) {
this.text = text;
}
public static String allImpresion() throws ClassNotFoundException{
StringBuilder all_impresion = new StringBuilder();
Class.forName("com.mysql.jdbc.Driver");
try (java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/guestbook", "root", "");) {
Statement st = conn.createStatement();
st.executeQuery("select username, text from impresion");
ResultSet rs = st.getResultSet();
while(rs.next()) {
all_impresion.append(rs.getString("username"));
all_impresion.append(": ");
all_impresion.append(rs.getString("text"));
all_impresion.append("\n");
}
} catch (SQLException ex) {
all_impresion.append(ex.getMessage());
}
return all_impresion.toString();
}
public void insertImpresion() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
try (java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/guestbook", "root", "");) {
if(username != null && !(username.isEmpty()) && text !=null && !(text.isEmpty())) {
Statement st = conn.createStatement();
st.execute("insert into impresion (username, text) value ('" +username +"', '" + text + "')");
}
} catch (SQLException ex) {
System.out.println("Error in database connection: \n"+ ex.getMessage());
}
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Guestbook</title>
</head>
<body>
<h1>Guestbook</h1>
<form:form action="impression.htm" method="post" commandName="impresion" >
<form:label path="username" > Enter your name: </form:label><br/>
<form:input id="name" type="text" path="username" placeholder="your name..."></form:input><br/>
<form:label path="text" > Enter your impresion: </form:label><br/>
<form:textarea id="impresion" path="text" placeholder="your impresion..." rows="4" cols="50"></form:textarea><br/>
<input type="submit" value="Submit" />
</form:form>
<label for="impresion_list" id="impresion_list_label">All impresion</label><br />
<textarea id="impresion_list" rows="20" cols="100" readonly>${impressions}</textarea>
</body>
</html>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import java.sql.SQLException;
import javax.validation.Valid;
import model.Impression;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ImpressionController {
@RequestMapping(value="/impression", method = RequestMethod.GET)
public String createForm(ModelMap model) throws ClassNotFoundException, SQLException {
model.addAttribute("impression", new Impression());
model.addAttribute("impressions", Impression.allImpresion());
return "impresion";
}
@RequestMapping(value="/impresion", method = RequestMethod.POST)
public String allImpresion(@ModelAttribute("impresion") @Valid Impression impression, ModelMap model) throws ClassNotFoundException, SQLException {
impression.insertImpresion();
createForm(model);
return "impresion";
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Impression {
private int id;
private String username;
private String text;
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getText() {
return text;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setText(String text) {
this.text = text;
}
public static String allImpresion() throws ClassNotFoundException{
StringBuilder all_impresion = new StringBuilder();
Class.forName("com.mysql.jdbc.Driver");
try (java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/guestbook", "root", "");) {
Statement st = conn.createStatement();
st.executeQuery("select username, text from impresion");
ResultSet rs = st.getResultSet();
while(rs.next()) {
all_impresion.append(rs.getString("username"));
all_impresion.append(": ");
all_impresion.append(rs.getString("text"));
all_impresion.append("\n");
}
} catch (SQLException ex) {
all_impresion.append(ex.getMessage());
}
return all_impresion.toString();
}
public void insertImpresion() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
try (java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/guestbook", "root", "");) {
if(username != null && !(username.isEmpty()) && text !=null && !(text.isEmpty())) {
Statement st = conn.createStatement();
st.execute("insert into impresion (username, text) value ('" +username +"', '" + text + "')");
}
} catch (SQLException ex) {
System.out.println("Error in database connection: \n"+ ex.getMessage());
}
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Guestbook</title>
</head>
<body>
<h1>Guestbook</h1>
<form:form action="impression.htm" method="post" commandName="impresion" >
<form:label path="username" > Enter your name: </form:label><br/>
<form:input id="name" type="text" path="username" placeholder="your name..."></form:input><br/>
<form:label path="text" > Enter your impresion: </form:label><br/>
<form:textarea id="impresion" path="text" placeholder="your impresion..." rows="4" cols="50"></form:textarea><br/>
<input type="submit" value="Submit" />
</form:form>
<label for="impresion_list" id="impresion_list_label">All impresion</label><br />
<textarea id="impresion_list" rows="20" cols="100" readonly>${impressions}</textarea>
</body>
</html>
Dragan