简体   繁体   中英

MVC Crud Operations not working

I'm performing emp CRUD operations with save and update operations using
Spring MVC, using SimpleUrlHandlerMapping. But when I run the project it is
throwing an error called /WEB-INF/MAC-servlet.xml not found. I created the above xml file in WEB-INF itself. Please solve this one. I added all Spring JARS along with MySQL connector. I'm using MySQL database to perform the operations.This is the following code of my project

empsave.jsp
_______________
<h1>Employee Save Form</h1>
<form action="./empsave.ds">
<pre>
Name:       <input type="text" name="name"/>
Email:      <input type="text" name="email"/>
Address:    <input rows="5" cols="15" name="address"/>
<input type="submit" value="save"/>
</pre>
</form>

web.xml
_______________
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
version="3.1">
<display-name>EmpCRUDOperations1</display-name>
<welcome-file-list>
   <welcome-file>empsave.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
 <servlet-name>MAC</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>MAC</servlet-name>
 <url-pattern>*.ds</url-pattern>
 </servlet-mapping>
</web-app>

empupdate.jsp
_________________
<h1>Employee Update Form</h1>
<form action="./empsave.ds">
<pre>
ID:         <input type="text" name="ID"/>
Name:       <input type="text" name="name"/>
Email:      <input type="text" name="email"/>
Address:    <input rows="5" cols="15" name="address"/>
<input type="submit" value="save"/>
</pre>
</form>

EmpCRUDController.java
_______________________
package controller;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class EmpCRUDController extends MultiActionController {

public ModelAndView empsave(HttpServletRequest req, HttpServletResponse 
res) throws Exception {

    String name=req.getParameter("name");
    String email=req.getParameter("email");
    String address=req.getParameter("address");
    Statement stmt=null;
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection con= DriverManager.getConnection
("jdbc:mysql://localhost/loginuser", "root", "root");
    ResultSet rs=con.createStatement().executeQuery("select max(id) 
from emp");
    int maxid=0;
    if(rs.next()){
        maxid=rs.getInt(1);
        maxid++;
    }
    PreparedStatement ps=(PreparedStatement) con.prepareStatement
("insert into emp values(?,?,?,?)");
    ps.setInt(1,maxid);
    ps.setString(2, name);
    ps.setString(3, email);
    ps.setString(4, address);
    int i=ps.executeUpdate();
    con.close();
    ModelAndView mav=null;
    if(i!=0)
        mav=new ModelAndView("success");
    else
        mav=new ModelAndView("fail");
    return mav;
}

public ModelAndView empupdate(HttpServletRequest req, 
HttpServletResponse res) throws Exception {

    int id=Integer.parseInt("id");
    String name=req.getParameter("name");
    String email=req.getParameter("email");
    String address=req.getParameter("address");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=(Connection) DriverManager.getConnection
("jdbc:mysql://localhost/loginuser", "root", "root");
    PreparedStatement ps=(PreparedStatement) con.prepareStatement
("update emp set name=?,email=?,address=?");
    ps.setString(1, name);
    ps.setString(2, email);
    ps.setString(3, address);
    int i=ps.executeUpdate();
    con.close();
    ModelAndView mav=null;
    if(i!=0)
        mav=new ModelAndView("success");
    else
        mav=new ModelAndView("fail");
    return mav;
}
}

MAC-servlet.xml
_________________
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean 
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/empsave.ds">EmpCRUD</prop>
<prop key="/empupdate.ds">EmpCRUD</prop>
</props>
</property>
</bean>
<bean id="EmpCRUD" class="controller.EmpCRUDController"/>
<bean class="org.springframework.web.servlet.view.
 InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"></property>
</bean>

success.jsp
____________
Save or Update Successfull....

fail.jsp
_________________-
Save or Update fail....

Code is correct pls add proper jar file and all jar file copy to lib folder i try same code but connected oracle 11g database working fine. jar list is shown below

罐子清单

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM