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


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

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

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

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

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

    效果如下:

    代码如下:

     1 package javaBean;
     2 
     3 public class Score {
     4     private int javaScore;
     5     private int j2eeScore;
     6     private int ccScore;
     7     private double aveScore;
     8     public int getJavaScore() {
     9         return javaScore;
    10     }
    11     public void setJavaScore(int javaScore) {
    12         this.javaScore = javaScore;
    13     }
    14     public int getJ2eeScore() {
    15         return j2eeScore;
    16     }
    17     public void setJ2eeScore(int j2eeScore) {
    18         this.j2eeScore = j2eeScore;
    19     }
    20     public int getCcScore() {
    21         return ccScore;
    22     }
    23     public void setCcScore(int ccScore) {
    24         this.ccScore = ccScore;
    25     }
    26     public double getAveScore() {
    27         return aveScore;
    28     }
    29     public void setAveScore(double aveScore) {
    30         this.aveScore = aveScore;
    31     }
    32 }
    Score
     1 package javaBean;
     2 
     3 
     4 public class Student {
     5     private String name;
     6     private long number;
     7     private Score score;
     8     public String getName() {
     9         return name;
    10     }
    11     public void setName(String name) {
    12         this.name = name;
    13     }
    14     public long getNumber() {
    15         return number;
    16     }
    17     public void setNumber(long number) {
    18         this.number = number;
    19     }
    20     public Score getScore() {
    21         return score;
    22     }
    23     public void setScore(Score score) {
    24         this.score = score;
    25     }
    26 }
    Student
     1 package actions;
     2 
     3 import java.math.BigDecimal;
     4 import java.util.List;
     5 
     6 import com.opensymphony.xwork2.ActionSupport;
     7 
     8 import javaBean.Score;
     9 import javaBean.Student;
    10 
    11 public class ScoreAction extends ActionSupport{
    12     private static final long serialVersionUID = 1L;
    13     private List<Student> students;
    14 
    15     public List<Student> getStudents() {
    16         return students;
    17     }
    18 
    19     public void setStudents(List<Student> students) {
    20         this.students = students;
    21     }
    22     public String execute() throws Exception{
    23         int size = students.size();
    24         for(int i=0; i<size; i++){
    25             Student st = students.get(i);
    26             Score score = st.getScore();
    27             double aveScore = (score.getJavaScore() + score.getCcScore() +
    28                     score.getJ2eeScore()) / 3.0;
    29             BigDecimal b = new BigDecimal(aveScore);
    30             aveScore = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    31             score.setAveScore(aveScore);
    32         }
    33         return super.SUCCESS;
    34     }
    35 }
    ScoreAction
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib prefix="s" uri="/struts-tags"%>
     3 <%
     4     String path = request.getContextPath();
     5     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
     6             + path + "/";
     7 %>
     8 
     9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10 <html>
    11 <head>
    12 <base href="<%=basePath%>">
    13 
    14 <title>输入学生成绩</title>
    15 
    16 <s:head theme="xhtml" />
    17 <style type="text/css">
    18 table {
    19     border-collapse: collapse;
    20     border: 1px solid #000;
    21 }
    22 
    23 th, td {
    24     border: 1px solid #000;
    25 }
    26 </style>
    27 </head>
    28 <body>
    29     <s:form theme="simple" action="showscores" namespace="/"   >
    30         <table>
    31             <thead>
    32                 <tr>
    33                     <th align="center">姓名</th>
    34                     <th align="center">学号</th>
    35                     <th align="center">Java成绩</th>
    36                     <th align="center">C语言成绩</th>
    37                     <th align="center">J2EE成绩</th>
    38                 </tr>
    39             </thead>
    40             <tbody>
    41                 <s:iterator value="new int[4]" status="st">
    42                     <tr>
    43                         <td><s:textfield name="%{'students['+#st.index+'].name'}"></s:textfield>
    44                         </td>
    45                         <td><s:textfield name="%{'students['+#st.index+'].number'}"></s:textfield>
    46                         </td>
    47                         <td><s:textfield
    48                                 name="%{'students['+#st.index+'].score.javaScore'}"></s:textfield>
    49                         </td>
    50                         <td><s:textfield
    51                                 name="%{'students['+#st.index+'].score.ccScore'}"></s:textfield></td>
    52                         <td><s:textfield
    53                                 name="%{'students['+#st.index+'].score.j2eeScore'}"></s:textfield>
    54                         </td>
    55                     </tr>
    56                 </s:iterator>
    57             </tbody>
    58         </table>
    59         <s:submit value="提交"></s:submit>
    60     </s:form>
    61 </body>
    62 </html>
    scores.jsp
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib prefix="s" uri="/struts-tags"%>
     3 <%
     4     String path = request.getContextPath();
     5     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
     6             + path + "/";
     7 %>
     8 
     9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10 <html>
    11 <head>
    12 <base href="<%=basePath%>">
    13 
    14 <title>显示学生信息</title>
    15 <s:head theme="xhtml" />
    16 <style type="text/css">
    17 table {
    18     border-collapse: collapse;
    19     border: 1px solid #000;
    20 }
    21 
    22 th, td {
    23     border: 1px solid #000;
    24 }
    25 </style>
    26 </head>
    27 
    28 <body>
    29     <table>
    30         <thead>
    31             <tr>
    32                 <th align="center">姓名</th>
    33                 <th align="center">学号</th>
    34                 <th align="center">Java成绩</th>
    35                 <th align="center">C语言成绩</th>
    36                 <th align="center">J2EE成绩</th>
    37                 <th align="center">平均成绩</th>
    38             </tr>
    39         </thead>
    40         <tbody>
    41             <s:iterator value="students" status="st">
    42                 <tr>
    43                     <td align="center"><s:property value="name" /></td>
    44                     <td align="center"><s:property value="number" /></td>
    45                     <td align="center"><s:property value="score.javaScore" /></td>
    46                     <td align="center"><s:property value="score.ccScore" /></td>
    47                     <td align="center"><s:property value="score.j2eeScore" /></td>
    48                     <td align="center"><s:property value="score.aveScore" /></td>
    49                 </tr>
    50             </s:iterator>
    51 
    52         </tbody>
    53     </table>
    54 </body>
    55 </html>
    showScores.jsp
    1 <action name="scores">
    2             <result>/scores.jsp</result>
    3 </action>
    4 <action name="showscores" class="actions.ScoreAction">
    5             <result name="success">/showScores.jsp</result>
    6 </action>
    struts.xml
  • 相关阅读:
    (OK) angular2-data-table is a Angular2 component for presenting large and complex data.
    Table pagination and Search bar in Angular2
    Angular 2 Tutorial: Create a CRUD App with Angular CLI and TypeScript
    Angular 2 CRUD application using Nodejs
    kernel 3.10内核源码分析--BUG_ON流程
    Protecting Routes using Guards in Angular 2
    Angular 2 User Registration and Login Example & Tutorial
    dmidecode
    (half OK) 在VirtualBox中运行 cm-13-kiwi (华为 荣耀 5X)
    Linux基金会宣布成立OpenSDS组织,应对存储云化转型
  • 原文地址:https://www.cnblogs.com/flyuz/p/9923914.html
Copyright © 2020-2023  润新知