实验五:struts2的应用
一.内容:在数据库里建立表格,编写模糊查询图书的应用,输入图书名称的模糊资料,显示查询的图书的ID,名字,价格,使用struts2完成
- 目的:掌握struts2框架的基本使用方法
三.
(本实验使用MySQL数据库)
![16075e80031f5b14fe7b02a3b4dbab0e.png](https://i-blog.csdnimg.cn/blog_migrate/9afc63caa65a30af653c73174aeb526c.jpeg)
![0da0c39d2dd3d60e36e1ae1335c043e0.png](https://i-blog.csdnimg.cn/blog_migrate/7ad05ad7145b94888b963c316253353e.png)
![9cd183ee4d5b231a64ee78254ca91d77.png](https://i-blog.csdnimg.cn/blog_migrate/41472c9cd7991dd658aa5108637a2b7c.jpeg)
- struts2配置文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="cn.rw.LoginAction">
<result name="success"> /bookList.jsp </result>
</action>
</package>
</struts>
- 编写 jsp
bookList.jsp login.jsp
bookList.jsp
<%@ page language="java" import="java.util.*,entity.*,cn.rw.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>图书列表</title>
</head>
<body>
<%
List<Book> books=(List)request.getAttribute("books");
%>
<center><font size="20">图书列表</font></center>
<hr/>
<table align="center" width="80%" border="1" cellspacing="0" cellpadding="5">
<tr><td>书号</td><td>书名</td><td>价格</td></tr>
<% if(books!=null){
for(Book b:books){
out.println("<tr><td>"+b.getId()+"</td><td>"+b.getName()
+"</td><td>"+b.getPrice()+"</td></tr>");
}
}else{
out.println("<tr><td colspan='3'>没有符合要求的图书</td></tr>");
}
%>
</table>
<center><a href="login.jsp"><font size="5" color="pink">继续查询</font>></a></center>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="/prj06/login.action" method="post">
请输入要查询的图书的名称:<input type="text" name="bname">
<input type="submit" value="提交">
</form>
</body>
</html>
- 编写并配置 LoginAction LoginAction.java
package cn.rw;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import entity.Book;
public class LoginAction {
private String bName;
public String getBname() {
return bName;
}
public void setBname(String bname) {
this.bName = bname;
}
public String execute() throws SQLException{
HttpServletRequest req=ServletActionContext.getRequest();
List<Book> books = new ArrayList<Book>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/product", "root", "root");
Statement stat = conn.createStatement();
String sql="select * from book where 1=1";
if(bName!=null){
sql =sql+ " and name like '%"+bName+"%'";
}
System.out.println(sql);
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setPrice(rs.getDouble("price"));
books.add(book);
}
req.setAttribute("books", books) ;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
}
- 编写实体类 (即 JavaBean) Book.java
package entity;
public class Book {
private int id;
private String name;
private double price;
public Book() {
}
public Book(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
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;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}