我使用
使用保存和更新操作执行emp CRUD操作
Spring MVC,使用SimpleUrlHandlerMapping。但是,当我运行项目时,它是
抛出一个名为/WEB-INF/MAC-servlet.xml的错误。我创造了
在WEB-INF本身的xml文件之上。请解决这个问题。我添加了所有Spring
JARS和MySQL连接器。我正在使用MySQL数据库来执行
operations.This是我项目的以下代码
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....