因为这是一个简单的管理系统,主页非常的烂,但是能用,先写一个主页的jsp页面名为,index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="基于SH学生信息管理-起始页"/></title>
</head>
<body bgcolor="#CCCCFF">
<s:div align="center">
<br/><br/><br/><br/><br/>
<font color="black" size="6">基于Struts2+Hibernate的学生信息管理系统实例,可对学生信息进行增、删、改、查!</font>
<br/><br/><br/>
<s:a href="lookMessageAction"><font color="blue" size="6">点此进入</font></s:a>
</s:div>
</body>
</html>
当我们点击 点此进入时会调用lookMessageAction方法
我们配置一下struts2的配置文件,这是配置完所有的Action后的代码,<result>中,当调用方法成功后,如果返回值与name中的字符串相同,就会跳转相应的页面
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="lookMessageAction" class="studentAction.LookMessageAction">
<result name="success">/student/lookMessage.jsp</result>
<result name="input">/student/index.jsp</result>
</action>
<action name="addMessageAction" class="studentAction.AddMessageAction">
<result name="success" type="chain">lookMessageAction</result>
<result name="input">/student/addMessage.jsp</result>
</action>
<action name="findMessageAction" class="studentAction.FindMessageAction">
<result name="success">/student/updateMessage.jsp</result>
<result name="input">/student/findMessage.jsp</result>
</action>
<action name="updateMessageAction" class="studentAction.UpdateMessageAction">
<result name="success" type="chain">lookMessageAction</result>
<result name="input">/student/updateMessage.jsp</result>
</action>
<action name="deleteMessageAction" class="studentAction.DeleteMessageAction">
<result name="success" type="chain">lookMessageAction</result>
<result name="input">/student/deleteMessage.jsp</result>
</action>
</package>
</struts>
在studentAction包下新建LookMessageAction.java.当我们的<action />不指定特定的方法时,默认执行一个返回值为string的extends方法。此类继承与ActionSupport类,代码相当简单。
先是通过继承于ServletRequest的接口HttpServletRequest取得request对象,然后通过dao层得到一个数据列表,然后通过setAttribute()方法把相应属性设置
package studentAction;
import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class LookMessageAction extends ActionSupport{
private HttpServletRequest request;
private String message="input";
public String execute() throws Exception{
request=ServletActionContext.getRequest();
StudentDao dao=new StudentDao();
List list=dao.findAllInfo();
request.getSession().setAttribute("count", list.size());
request.getSession().setAttribute("allInfo", list);
message="success";
return message;
}
}
下面来写一个lookMessage.jsp,在页面显示学生信息的页面,此页面有几个链接,分别链接到不同的页面。然后通过getAttribute()方法获取count的值,即学生的总数、然后用同样的方法获取全部信息的一个列表。再通过JS代码一一把学生信息显示到页面上。
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.ArrayList,PO.Stuinfo"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="学生信息管理系统-查看"/></title>
</head>
<body bgcolor="pink">
<s:div align="center">
<hr color="red"/>
<br/>
<table align="center" width="80%">
<tr>
<td width="25%">
查看学生信息
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/addMessage.jsp">添加学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/findMessage.jsp">修改学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/deleteMessage.jsp">删除学生信息</s:a>
</td>
</tr>
</table>
<br/>
<hr color="red"/>
<br/><br/><br/>
<span>你要查询的数据表中共有<%=request.getSession().getAttribute("count")%>人</span>
</s:div>
<table align="center" width="80%" border="5">
<tr>
<th>记录条数</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>体重</th>
</tr>
<%
ArrayList list=(ArrayList)session.getAttribute("allInfo");
if(list.isEmpty()){
%>
<tr>
<td align="center">
<span>暂无学生信息!</span>
</td>
</tr>
<%
}else{
for(int i=0;i<list.size();i++){
Stuinfo info=(Stuinfo)list.get(i);
%>
<tr>
<td align="center"><%=i+1%></td>
<td><%=info.getId()%></td>
<td><%=info.getName()%></td>
<td><%=info.getSex()%></td>
<td><%=info.getAge()%></td>
<td><%=info.getWeight()%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
当我们点击index.jsp页面的添加学生信息的时候,就会转入相应的页面。写一个addMessage.jsp.此页面中表单对应的是addMessageAction方法中的execute()方法
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="学生信息管理系统-增加"/></title>
</head>
<body bgcolor="pink">
<s:div align="center">
<hr color="red"/>
<br/>
<table align="center" width="80%">
<tr>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/lookMessage.jsp">查看学生信息</s:a>
</td>
<td width="25%">
添加学生信息
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/findMessage.jsp">修改学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/deleteMessage.jsp">删除学生信息</s:a>
</td>
</tr>
</table>
<br/>
<hr color="red"/>
<center><font color="red" size="6">添加学生信息</font></center>
</s:div>
<s:form action="addMessageAction" method="post">
<table align="center" width="30%" bgcolor="gray" border="5">
<tr>
<td>
<s:textfield name="id" label="学号" maxLength="16"></s:textfield>
</td>
<td>
<s:textfield name="name" label="姓名" maxLength="16"/>
</td>
<td>
<s:select name="sex" label="性别" list="{'男','女'}"/>
</td>
<td>
<s:textfield name="age" label="年龄"/>
</td>
<td>
<s:textfield name="weight" label="体重"/>
</td>
<td colspan="2">
<s:submit value="提交"/>
<s:reset value="清除"/>
</td>
</tr>
</table>
</s:form>
</body>
</html>
新建一个AddMessageAction.java。大多都是一些set/get方法。切记每个get方法中的参数一定要于对应jsp页面textfield中name中的值一样。不然就无法传值
我们实用validate()方法验证我们jsp中的数据。然后info()方法把填入的数据封装为一个学生对象返回。并且在execute()方法用dao层把数据添加到数据库、
package studentAction;
import Dao.StudentDao;
import PO.Stuinfo;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.swing.JOptionPane;
public class AddMessageAction extends ActionSupport{
private String id;
private String name;
private String sex;
private int age;
private float weight;
private String message="input";
public String getId() {
return id;
}
public void setId(String 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;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public void validate(){
if(this.getId()==null||this.getId().length()==0){
addFieldError("id","学号不允许为空!");
}else{
StudentDao dao=new StudentDao();
List list=dao.findInfo("id", this.getId());
if(!list.isEmpty()){
addFieldError("id","学号已存在!");
}
}
if(this.getName()==null||this.getName().length()==0){
addFieldError("name","姓名不允许为空!");
}
if(this.getAge()>130){
addFieldError("age","请认真核实年龄!");
}
if(this.getWeight()>500){
addFieldError("weight","请认真核实体重!");
}
}
public String execute() throws Exception{
StudentDao dao=new StudentDao();
boolean save=dao.saveInfo(info());
if(save){
message="success";
}
return message;
}
public Stuinfo info(){
Stuinfo info=new Stuinfo();
info.setId(this.getId());
info.setName(this.getName());
info.setSex(this.getSex());
info.setAge(this.getAge());
info.setWeight(this.getWeight());
return info;
}
public void message(String mess){
int type=JOptionPane.YES_NO_OPTION;
String title="提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}
当我们要删除或修改学生信息的时候必须先找到要删除或修改的数据,。所以,接下来,我们写一个findMessage.jsp。一个<select></select>标签,就是把所有学生信息的学号显示出来。可以选择。然后可以删除或修改
<%@page import="java.util.ArrayList"%>
<%@page import="PO.Stuinfo"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="学生信息管理系统-查找"/></title>
</head>
<body bgcolor="pink">
<s:div align="center">
<hr color="red"/>
<br/>
<table align="center" width="80%">
<tr>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/lookMessage.jsp">查看学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/addMessage.jsp">添加学生信息</s:a>
</td>
<td width="25%">
修改学生信息
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/deleteMessage.jsp">删除学生信息</s:a>
</td>
</tr>
</table>
<br/>
<hr color="red"/>
<br/><br/><br/>
<font size="5">修改学生信息</font>
</s:div>
<s:form action="findMessageAction" method="post">
<table align="center" width="40%" border="5">
<tr>
<td>
请选择要修改学生的学号:
</td>
<td>
<select name="id">
<%
ArrayList list=(ArrayList)session.getAttribute("allInfo");
if(list.isEmpty()){
%>
<option value="null">null</option>
<%
}else{
for(int i=0;i<list.size();i++){
Stuinfo info=(Stuinfo)list.get(i);
%>
<option value="<%=info.getId()%>"><%=info.getId()%></option>
<%
}
}
%>
</select>
</td>
<td>
<s:submit value="确定"></s:submit>
</td>
</tr>
</table>
</s:form>
</body>
</html>
下面写FindMessageAction.java。主要是对id进行操作。然后通过request对象设置数据等
package studentAction;
import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.ServletActionContext;
public class FindMessageAction extends ActionSupport{
private String id;
private HttpServletRequest request;
private String message="input";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void validate(){
if(this.getId().equals("null")){
message("暂无学生信息!");
addFieldError("id","暂无学生信息!");
}
}
public String execute() throws Exception{
request=ServletActionContext.getRequest();
StudentDao dao=new StudentDao();
List list=dao.findInfo("id", this.getId());
request.getSession().setAttribute("oneInfo", list);
message="success";
return message;
}
public void message(String mess){
int type=JOptionPane.YES_NO_OPTION;
String title="提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}
下面我们再写删除操作的.jsp和.java
<%@page import="PO.Stuinfo"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="学生信息管理系统-删除"/></title>
</head>
<body bgcolor="pink">
<s:div align="center">
<hr color="red"/>
<br/>
<table align="center" width="80%">
<tr>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/lookMessage.jsp">查看学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/addMessage.jsp">添加学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/findMessage.jsp">修改学生信息</s:a>
</td>
<td width="25%">
删除学生信息
</td>
</tr>
</table>
<br/>
<hr color="red"/>
<br/><br/><br/>
<font size="5">删除学生信息</font>
</s:div>
<s:form action="deleteMessageAction" method="post">
<table align="center" width="40%" border="5">
<tr>
<td>
请选择要删除学生的学号:
</td>
<td>
<select name="id">
<%
ArrayList list=(ArrayList)session.getAttribute("allInfo");
if(list.isEmpty()){
%>
<option value="null">null</option>
<%
}else{
for(int i=0;i<list.size();i++){
Stuinfo info=(Stuinfo)list.get(i);
%>
<option value="<%=info.getId()%>"><%=info.getId()%></option>
<%
}
}
%>
</select>
</td>
<td>
<s:submit value="确定"></s:submit>
</td>
</tr>
</table>
</s:form>
</body>
</html>
删除的java为。首先是找到学生的id。然后利用dao层的deleteInfo()方法删除学生信息
package studentAction;
import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import javax.swing.JOptionPane;
public class DeleteMessageAction extends ActionSupport{
private String id;
private String message;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void validate(){
if(this.getId().equals("null")){
message("暂无学生信息!");
addFieldError("id","暂无学生信息!");
}
}
public String execute() throws Exception{
StudentDao dao=new StudentDao();
boolean del=dao.deleteInfo(this.getId());
if(del){
message="success";
}
return message;
}
public void message(String mess){
int type=JOptionPane.YES_NO_OPTION;
String title="提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}
最后就是我们的修改操作
.jsp
<%@page import="PO.Stuinfo"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="学生信息管理系统-修改"/></title>
</head>
<body bgcolor="pink">
<s:div align="center">
<hr color="red"/>
<br/>
<table align="center" width="80%">
<tr>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/lookMessage.jsp">查看学生信息</s:a>
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/addMessage.jsp">添加学生信息</s:a>
</td>
<td width="25%">
修改学生信息
</td>
<td width="25%">
<s:a href="http://localhost:8080/ch06/student/deleteMessage.jsp">删除学生信息</s:a>
</td>
</tr>
</table>
<br/>
<hr color="red"/>
<br/><br/><br/>
<font size="5">修改学生信息</font>
</s:div>
<s:form action="updateMessageAction" method="post">
<table align="center" width="30%" bgcolor="gray" border="5">
<%
ArrayList list=(ArrayList)session.getAttribute("oneInfo");
Stuinfo info=(Stuinfo)list.get(0);
%>
<tr>
<td>
学号
</td>
<td>
<input name="id" value="<%=info.getId()%>" readonly="readonly"/>
</td>
</tr>
<tr>
<td>
姓名
</td>
<td>
<input name="name" value="<%=info.getName()%>"/>
</td>
</tr>
<tr>
<td>
性别
</td>
<td>
<input name="sex" value="<%=info.getSex()%>"/>
</td>
</tr>
<tr>
<td>
年龄
</td>
<td>
<input name="age" value="<%=info.getAge()%>"/>
</td>
</tr>
<tr>
<td>
体重
</td>
<td>
<input name="weight" value="<%=info.getWeight()%>"/>
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="提交"></s:submit>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<s:a href="http://localhost:8080/ch06/student/findMessage.jsp">返回</s:a>
</td>
</tr>
</table>
</s:form>
</body>
</html>
.java
package studentAction;
import Dao.StudentDao;
import PO.Stuinfo;
import com.opensymphony.xwork2.ActionSupport;
import javax.swing.JOptionPane;
public class UpdateMessageAction extends ActionSupport{
private String id;
private String name;
private String sex;
private int age;
private float weight;
private String message="input";
public String getId() {
return id;
}
public void setId(String 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;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public void validate(){
if(this.getName()==null||this.getName().length()==0){
addFieldError("name","姓名不允许为空!");
}
if(this.getAge()>130){
addFieldError("age","请认真核实年龄!");
}
if(this.getWeight()>500){
addFieldError("weight","请认真核实体重!");
}
}
public String execute() throws Exception{
StudentDao dao=new StudentDao();
boolean update=dao.updateInfo(info());
if(update){
message="success";
}
return message;
}
public Stuinfo info(){
Stuinfo info=new Stuinfo();
info.setId(this.getId());
info.setName(this.getName());
info.setSex(this.getSex());
info.setAge(this.getAge());
info.setWeight(this.getWeight());
return info;
}
public void message(String mess){
int type=JOptionPane.YES_NO_OPTION;
String title="提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}
这样我们发布在本地的Tomcat上,就可以访问页面了。
这个小应用描述的比较粗糙。由于刚接触。知识面也不广。基本上代码都能读懂,然后也里面的内容都懂。。
在我运行发布这个web应用时,遇到了一个问题。就是,Tomcat OutOfMemoryError:PermGen space 。大概意思就是说内存溢出了吧