类Score.java:各课程的成绩及平均成绩

类Student.java:学生姓名、学号及Score类

类ScoreAction.java:将Student类存在一个List对象中, execute()方法根据用户输入的成绩计算每个学生的平均成绩。

页面scores.jsp:完成录入学生信息及考试成绩

页面showScores.jsp:显示已录入的学生成绩及平均成绩

效果如下:

javaee--学生成绩录入与显示--Struts2标签的使用-LMLPHP

javaee--学生成绩录入与显示--Struts2标签的使用-LMLPHP

代码如下:

 package javaBean;

 public class Score {
private int javaScore;
private int j2eeScore;
private int ccScore;
private double aveScore;
public int getJavaScore() {
return javaScore;
}
public void setJavaScore(int javaScore) {
this.javaScore = javaScore;
}
public int getJ2eeScore() {
return j2eeScore;
}
public void setJ2eeScore(int j2eeScore) {
this.j2eeScore = j2eeScore;
}
public int getCcScore() {
return ccScore;
}
public void setCcScore(int ccScore) {
this.ccScore = ccScore;
}
public double getAveScore() {
return aveScore;
}
public void setAveScore(double aveScore) {
this.aveScore = aveScore;
}
}

Score

 package javaBean;

 public class Student {
private String name;
private long number;
private Score score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
}

Student

 package actions;

 import java.math.BigDecimal;
import java.util.List; import com.opensymphony.xwork2.ActionSupport; import javaBean.Score;
import javaBean.Student; public class ScoreAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private List<Student> students; public List<Student> getStudents() {
return students;
} public void setStudents(List<Student> students) {
this.students = students;
}
public String execute() throws Exception{
int size = students.size();
for(int i=0; i<size; i++){
Student st = students.get(i);
Score score = st.getScore();
double aveScore = (score.getJavaScore() + score.getCcScore() +
score.getJ2eeScore()) / 3.0;
BigDecimal b = new BigDecimal(aveScore);
aveScore = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
score.setAveScore(aveScore);
}
return super.SUCCESS;
}
}

ScoreAction

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>输入学生成绩</title> <s:head theme="xhtml" />
<style type="text/css">
table {
border-collapse: collapse;
border: 1px solid #000;
} th, td {
border: 1px solid #000;
}
</style>
</head>
<body>
<s:form theme="simple" action="showscores" namespace="/" >
<table>
<thead>
<tr>
<th align="center">姓名</th>
<th align="center">学号</th>
<th align="center">Java成绩</th>
<th align="center">C语言成绩</th>
<th align="center">J2EE成绩</th>
</tr>
</thead>
<tbody>
<s:iterator value="new int[4]" status="st">
<tr>
<td><s:textfield name="%{'students['+#st.index+'].name'}"></s:textfield>
</td>
<td><s:textfield name="%{'students['+#st.index+'].number'}"></s:textfield>
</td>
<td><s:textfield
name="%{'students['+#st.index+'].score.javaScore'}"></s:textfield>
</td>
<td><s:textfield
name="%{'students['+#st.index+'].score.ccScore'}"></s:textfield></td>
<td><s:textfield
name="%{'students['+#st.index+'].score.j2eeScore'}"></s:textfield>
</td>
</tr>
</s:iterator>
</tbody>
</table>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>

scores.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>显示学生信息</title>
<s:head theme="xhtml" />
<style type="text/css">
table {
border-collapse: collapse;
border: 1px solid #000;
} th, td {
border: 1px solid #000;
}
</style>
</head> <body>
<table>
<thead>
<tr>
<th align="center">姓名</th>
<th align="center">学号</th>
<th align="center">Java成绩</th>
<th align="center">C语言成绩</th>
<th align="center">J2EE成绩</th>
<th align="center">平均成绩</th>
</tr>
</thead>
<tbody>
<s:iterator value="students" status="st">
<tr>
<td align="center"><s:property value="name" /></td>
<td align="center"><s:property value="number" /></td>
<td align="center"><s:property value="score.javaScore" /></td>
<td align="center"><s:property value="score.ccScore" /></td>
<td align="center"><s:property value="score.j2eeScore" /></td>
<td align="center"><s:property value="score.aveScore" /></td>
</tr>
</s:iterator> </tbody>
</table>
</body>
</html>

showScores.jsp

 <action name="scores">
<result>/scores.jsp</result>
</action>
<action name="showscores" class="actions.ScoreAction">
<result name="success">/showScores.jsp</result>
</action>

struts.xml

05-28 12:36