<form method=post action="addNew_hxxm.jsp" name="form1"enctype="multipart/form-data">

问题说明:当html页面的<form>属性中有enctype="multipart/form-data" 时 在jsp页面不能用request.getParameter("name");获得html表单中的数据

例:<form method=post action="addNew_hxxm.jsp" name="form1"enctype="multipart/form-data">

 

解决办法:在jsp页面的<body>下加上以下代码

<%

mySmartUpload.initialize(pageContext);
   mySmartUpload.service(request,response);
   mySmartUpload.upload();

String i_xmmc = mySmartUpload.getRequest().getParameter("xmmc");//取单个参数单个值

%>

 

关于enctype="multipart/form-data"的说明

表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作.

enctype="multipart/form-data"是上传二进制数据; 
form里面的input的值以2进制的方式传过去,所以request就得不到值了。

取表单值加入数据库时,用到下面的:

SmartUpload su = new SmartUpload();//新建一个SmartUpload对象

su.getRequest().getParameterValues();取数组值

su.getRequest().getParameter( );取单个参数单个值

 

 

好的,我可以给您提供一个Spring Boot框架下的示例代码,包含了添加书籍的分类管理、添加图书、查询书籍、预览和附件上传功能。请注意,这只是一个示例代码,您需要根据实际需求进行修改和完善。 1. 创建Maven工程,添加Spring Boot和Web依赖 ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- 文件上传依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> </dependencies> ``` 2. 编写Controller代码 ```java @Controller @RequestMapping("/book") public class BookController { @Autowired private BookService bookService; // 添加书籍分类 @PostMapping("/addCategory") public String addCategory(@RequestParam("name") String name) { bookService.addCategory(name); return "redirect:/book/category"; } // 查询书籍分类 @GetMapping("/category") public String category(Model model) { List<Category> categories = bookService.getCategories(); model.addAttribute("categories", categories); return "category"; } // 添加图书 @PostMapping("/addBook") public String addBook(@ModelAttribute Book book, @RequestParam("file") MultipartFile file) { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); book.setCover(fileName); bookService.addBook(book); String uploadDir = "book-covers/" + book.getId(); FileUploadUtil.saveFile(uploadDir, fileName, file); return "redirect:/book/list"; } // 查询图书列表 @GetMapping("/list") public String list(Model model) { List<Book> books = bookService.getBooks(); model.addAttribute("books", books); return "list"; } // 预览图书 @GetMapping("/preview/{id}") public String preview(@PathVariable("id") Long id, Model model) { Book book = bookService.getBookById(id); model.addAttribute("book", book); return "preview"; } } ``` 3. 编写Service接口和实现类 ```java public interface BookService { void addCategory(String name); List<Category> getCategories(); void addBook(Book book); List<Book> getBooks(); Book getBookById(Long id); } @Service public class BookServiceImpl implements BookService { @Autowired private CategoryRepository categoryRepository; @Autowired private BookRepository bookRepository; @Override public void addCategory(String name) { Category category = new Category(); category.setName(name); categoryRepository.save(category); } @Override public List<Category> getCategories() { return categoryRepository.findAll(); } @Override public void addBook(Book book) { bookRepository.save(book); } @Override public List<Book> getBooks() { return bookRepository.findAll(); } @Override public Book getBookById(Long id) { return bookRepository.findById(id).orElse(null); } } @Repository public interface BookRepository extends JpaRepository<Book, Long> { } @Repository public interface CategoryRepository extends JpaRepository<Category, Long> { } ``` 4. 编写实体类 ```java @Entity @Table(name = "category") public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // getter和setter方法省略 } @Entity @Table(name = "book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne @JoinColumn(name = "category_id") private Category category; private String author; private String description; private String cover; // getter和setter方法省略 } ``` 5. 编写Thymeleaf模板 category.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Book Category</title> </head> <body> <h2>Add Category</h2> <form method="post" action="/book/addCategory"> <label>Name:</label> <input type="text" name="name"/> <button type="submit">Add</button> </form> <hr> <h2>Category List</h2> <table border="1"> <tr> <th>ID</th> <th>Name</th> </tr> <tr th:each="category : ${categories}"> <td th:text="${category.id}"></td> <td th:text="${category.name}"></td> </tr> </table> </body> </html> ``` list.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Book List</title> </head> <body> <h2>Add Book</h2> <form method="post" action="/book/addBook" enctype="multipart/form-data"> <label>Name:</label> <input type="text" name="name"/><br> <label>Category:</label> <select name="category.id"> <option th:each="category : ${categories}" th:value="${category.id}" th:text="${category.name}"></option> </select><br> <label>Author:</label> <input type="text" name="author"/><br> <label>Description:</label> <textarea name="description"></textarea><br> <label>Cover:</label> <input type="file" name="file"/><br> <button type="submit">Add</button> </form> <hr> <h2>Book List</h2> <table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Category</th> <th>Author</th> <th>Description</th> <th>Cover</th> </tr> <tr th:each="book : ${books}"> <td th:text="${book.id}"></td> <td th:text="${book.name}"></td> <td th:text="${book.category.name}"></td> <td th:text="${book.author}"></td> <td th:text="${book.description}"></td> <td><img th:src="@{/image/book-covers/__${book.id}__/__${book.cover}__}" width="100"/></td> </tr> </table> </body> </html> ``` preview.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${book.name}"></title> </head> <body> <h2 th:text="${book.name}"></h2> <p th:text="${book.author}"></p> <p th:text="${book.description}"></p> <img th:src="@{/image/book-covers/__${book.id}__/__${book.cover}__}" width="300"/> </body> </html> ``` 6. 编写文件上传工具类 ```java public class FileUploadUtil { public static void saveFile(String uploadDir, String fileName, MultipartFile multipartFile) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } try (InputStream inputStream = multipartFile.getInputStream()) { Path filePath = uploadPath.resolve(fileName); Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { throw new IOException("Could not save file: " + fileName, ex); } } } ``` 以上就是一个简单的JavaWeb在线商城系统的示例代码,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值