<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
function add(){
location = "add.jsp";
}
function del(id){
location = "list!del?id="+id;
}
function upd(id){
location = "upd.jsp?id="+id;
}
</script>
</head>
<body>
<table border="1px">
<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
<th>操作|<input type="button" value="增加" onclick="add()"></th>
</tr>
<c:forEach items="${list}" var="s">
<tr>
<th>${s.id}</th>
<th>${s.name}</th>
<th>${s.sex}</th>
<th>${s.age}</th>
<th><input type="button" value="删除" onclick="del(${s.id})">
<input type="button" value="修改" onclick="upd(${s.id})"></th>
</tr>
</c:forEach>
</table>
</body>
</html>
<?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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package com.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.dto.Teacher;
import com.opensymphony.xwork2.ActionSupport;
import com.service.TeacherService;
public class Action extends ActionSupport{
private TeacherService service = new TeacherService();
List<Teacher> list;
private int id;
private String name;
private String sex;
private int age;
public String execute(){
list = service.list();
System.out.println(list);
return "tolist";
}
public String add() throws IOException{
int i = service.add(id,name,sex,age);
System.out.println(i);
HttpServletResponse response = ServletActionContext.getResponse();
response.getWriter().print(i);
return null;
}
public String del(){
int i = service.del(id);
System.out.println(i);
return SUCCESS;
}
public String show(){
Teacher t = service.show(id);
System.out.println(t);
return SUCCESS;
}
public List<Teacher> getList() {
return list;
}
public void setList(List<Teacher> list) {
this.list = list;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
读书不只是为了读书,
是为了更好的思考。
思考不只是为了思考,
是为了更有意义的生活。
——王户安