这是我的 Web 应用程序的控制器类。@Controller 公共类 BookController {
@Autowired
private BookService bookService;
@RequestMapping(value = "/save", method = RequestMethod.GET)
public ModelAndView saveEmployee(@ModelAttribute("command") BooksBean bookBean,
BindingResult result) {
Book book = prepareModel(bookBean);
bookService.addBook(book);
return new ModelAndView("redirect:/add.html");
}
@RequestMapping(value="/books", method = RequestMethod.GET)
public ModelAndView listBooks() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("books", prepareListofBean(bookService.listBooks()));
return new ModelAndView("booksList", model);
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addBook(@ModelAttribute("command") BooksBean booksBean,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("books", prepareListofBean(bookService.listBooks()));
return new ModelAndView("addBook", model);
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("index");
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView editBook(@ModelAttribute("command") BooksBean booksBean,
BindingResult result) {
bookService.deleteBook(prepareModel(booksBean));
Map<String, Object> model = new HashMap<String, Object>();
model.put("book", null);
model.put("books", prepareListofBean(bookService.listBooks()));
return new ModelAndView("addBook", model);
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView deleteBook(@ModelAttribute("command") BooksBean booksBean,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("book", prepareBooksBean(bookService.getBook(booksBean.getBookCode())));
model.put("books", prepareListofBean(bookService.listBooks()));
return new ModelAndView("addBook", model);
}
private Book prepareModel(BooksBean booksBean){
Book book = new Book();
book.setBookName(booksBean.getBookName());
book.setBookCode(booksBean.getBookCode());
book.setAuthor(booksBean.getAuthor());
book.setDateOfArrival(booksBean.getDateOfArrival());
book.setPrice(booksBean.getPrice());
book.setRackId(booksBean.getRackId());
book.setNumberOfBooks(booksBean.getNumberOfBooks());
book.setSubjectCode(booksBean.getSubjectCode());
return book;
}
private List<BooksBean> prepareListofBean(List<Book> books){
List<BooksBean> beans = null;
if(books != null && !books.isEmpty())
{
beans = new ArrayList<BooksBean>();
BooksBean bean = null;
for(Book book : books){
bean = new BooksBean();
bean.setBookName(book.getBookName());
bean.setBookCode(book.getBookCode());
bean.setAuthor(book.getAuthor());
bean.setDateOfArrival(book.getDateOfArrival());
bean.setPrice(book.getPrice());
bean.setRackId(book.getRackId());
bean.setNumberOfBooks(book.getNumberOfBooks());
bean.setSubjectCode(book.getSubjectCode());
beans.add(bean);
}
}
return beans;
}
private BooksBean prepareBooksBean(Book book){
BooksBean bean = new BooksBean();
bean.setBookName(book.getBookName());
bean.setBookCode(book.getBookCode());
bean.setAuthor(book.getAuthor());
bean.setDateOfArrival(book.getDateOfArrival());
bean.setPrice(book.getPrice());
bean.setRackId(book.getRackId());
bean.setNumberOfBooks(book.getNumberOfBooks());
bean.setSubjectCode(book.getSubjectCode());
return bean;
}
}
这是春季添加元素的页面。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>Add Employee Data</h2>
<form:form method="POST" action="/sdnext/save.html">
<table>
<tr>
<td><form:label path="bookName">Name Of Book:</form:label></td>
<td><form:input path="bookName" value="${book.bookName}" /></td>
</tr>
<tr>
<td><form:label path="bookCode">Book Code:</form:label></td>
<td><form:input path="bookCode" value="${book.bookCode}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="author">Book Author Name:</form:label></td>
<td><form:input path="author" value="${book.author}"/></td>
</tr>
<tr>
<td><form:label path="dateOfArrival">Date Of Arrival:</form:label></td>
<td><form:input path="dateOfArrival" value="${book.dateOfArrival}"/></td>
</tr>
这是一个包含映射的 web.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这个 sdnext-servlet.xml 文件用于定义上下文路径和 jsp 解析器和数据库处理程序。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.tcs" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.tcs.model.Book</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>