您的位置:首页 > 编程语言 > Java开发

springmvc学习笔记3

2016-06-30 00:04 357 查看
@CHARSET "UTF-8";

#global {
text-align: left;
border: 1px solid #dedede;
background: #efefef;
width: 560px;
padding: 20px;
margin: 100px auto;
}

form {
font:100% verdana;
min-width: 500px;
max-width: 600px;
width: 560px;
}

form fieldset {
border-color: #bdbebf;
border-width: 3px;
margin: 0;
}

legend {
font-size: 1.3em;
}

form label {
width: 250px;
display: block;
float: left;
text-align: right;
padding: 2px;
}

#buttons {
text-align: right;
}
#errors, li {
color: red;
}

<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="laotou99.controller"/>
<context:component-scan base-package="laotou99.service"/>

<mvc:annotation-driven/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/*.html" location="/"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!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=GBK">
<title>Add Book Form</title>
</head>
<body>
<div id="global">
<form:form commandName="book" action="book_save" method="post" >
<fieldset>
<legend> Add a book </legend>
<p>
<label for="category">Category:</label>
<form:select id="category" path="category.id"
items="${categories}" itemLabel="name" itemValue="id" />
</p>
<p>
<label for="title">Title: </label>
<form:input path="title" id="title" />
</p>
<p>
<label for="author">Author: </label>
<form:input path="author" id="author" />
</p>
<p>
<label for="isbn">ISBN: </label>
<form:input path="isbn" id="isbn" />
</p>
<p id="buttons">
<input id="rest" type="reset" tabindex="4">
<input di="submit" type="submit" tabindex="5" value="Add Book">
</p>
</fieldset>
</form:form>

</div>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ 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=GBK">
<title>Edit Book Form</title>
</head>
<body>
<div id="global">
<c:url var="formAction" value="/book_update"/>
<form:form commandName="book" action="${formAction}" method="post">
<fieldset>
<legend>Edit a book</legend>
<form:hidden path="id"/>
<p>
<label for="category">Category: </label>
<form:select path="category.id" id="category"
items="${categories}" itemLabel="name" itemValue="id" />
</p>
<p>
<label for="title">Title: </label>
<form:input path="title" id="title" />
</p>
<p>
<label for="author">Author: </label>
<form:input path="author" id="author" />
</p>
<p>
<label for="isbn">ISBN: </label>
<form:input path="isbn" id="isbn" />
</p>
<p id="buttons">
<input id="rest" type="reset" tabindex="4">
<input di="submit" type="submit" tabindex="5" value="Update Book">
</p>
</fieldset>
</form:form>
</div>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ 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=GBK">
<title>BOOK LIST</title>
<style type="text/css">
@import url("<c:url value="/css/main.css" />");
</style>
</head>
<body>
<div id="global">
<h1>Book List</h1>
<a href="<c:url value="/book_input" />">Add Book</a>
<table>
<tr>
<th>Category</th>
<th>Title</th>
<th>ISBN</th>
<th>Author</th>
<th> </th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.category.name}</td>
<td>${book.title}</td>
<td>${book.isbn}</td>
<td>${book.author}</td>
<td><a href="book_edit/${book.id}" >Edit</a></td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>


<?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>SMTest4</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

package laotou99.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import laotou99.domain.Book;
import laotou99.domain.Category;
import laotou99.service.BookService;

@Controller
public class BookController {

@Autowired
private BookService bookService;

private static final Log logger=LogFactory.getLog(BookController.class);

@RequestMapping(value="/book_input")
public String inputBook(Model model){
List<Category> categories = bookService.getAllCategories();
model.addAttribute("categories", categories);
model.addAttribute("book", new Book());
return "BookAddForm";
}

@RequestMapping(value="/book_edit/{id}")
public String editBook(Model model,@PathVariable long id){

List<Category> categories = bookService.getAllCategories();
model.addAttribute("categories", categories);
Book book = bookService.get(id);
model.addAttribute("book", book);

return "BookEditForm";
}

@RequestMapping(value="/book_save")
public String saveBook(@ModelAttribute Book book){

Category category =bookService.getCategory(book.getCategory().getId());
book.setCategory(category);
bookService.save(book);
return "redirect:/book_list";
}

@RequestMapping(value="/book_update")
public String updateBook(@ModelAttribute Book book){

Category category = bookService.getCategory(book.getCategory().getId());

book.setCategory(category);
bookService.update(book);
return "redirect:/book_list";
}

@RequestMapping(value="/book_list")
public String listBooks(Model model){

logger.info("book_list");
List<Book> books = bookService.getAllBooks();
model.addAttribute("books", books);

return "BookList";
}
}

package laotou99.domain;

import java.io.Serializable;

public class Book implements Serializable {

private static final long serialVersionUID = 8523748048578495695L;

private long id;
private String isbn;
private String title;
private Category category;
private String author;

public Book(){}

public Book(long id, String isbn, String title,
Category category, String author)
{
this.id = id;
this.isbn = isbn;
this.title = title;
this.category = category;
this.author = author;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}
}

package laotou99.domain;

import java.io.Serializable;

public class Category implements Serializable {

private static final long serialVersionUID = 7535732611636313900L;

private int id;
private String name;

public Category(){}

public Category(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

package laotou99.service;

import java.util.List;

import laotou99.domain.Book;
import laotou99.domain.Category;

public interface BookService {

List<Category> getAllCategories();
Category getCategory(int id);
List<Book> getAllBooks();
Book save(Book book);
Book update(Book book);
Book get(long id);
long getNextId();
}

package laotou99.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import laotou99.domain.Book;
import laotou99.domain.Category;

@Service
public class BookServiceImpl implements BookService {

private List<Category> categories;
private List<Book> books;

public BookServiceImpl() {

categories = new ArrayList<Category>();
Category category1 = new Category(1, "aaa");
Category category2 = new Category(2, "bbb");
Category category3 = new Category(3, "ccc");
categories.add(category1);
categories.add(category2);
categories.add(category3);

books = new ArrayList<Book>();
books.add(new Book(1L, "11111", "abc", category1, "www"));
books.add(new Book(2L, "11112", "abc2", category1, "www2"));
}

@Override
public List<Category> getAllCategories() {
return categories;
}

@Override
public Category getCategory(int id) {

for(Category category:categories){
if(id==category.getId()){
return category;
}
}
return null;
}

@Override
public List<Book> getAllBooks() {
return books;
}

@Override
public Book save(Book book) {
book.setId(getNextId());
books.add(book);

return book;
}

@Override
public Book update(Book book) {
int bookCount = books.size();
for(int i=0;i<bookCount;i++){
Book savedBook = books.get(i);
if(savedBook.getId()==book.getId()){
books.set(i, book);
return  book;
}
}
return book;
}

@Override
public Book get(long id) {
for(Book book:books){
if(id==book.getId()){
return book;
}
}
return null;
}

@Override
public long getNextId() {

long id = 0L;
for(Book book:books){
long bookId = book.getId();
if(bookId > id){
id = bookId;
}
}
return id + 1;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: