从Struts2中的特定URL重定向后显示特定的div

时间:2016-08-25 06:16:13

标签: jsp struts2 conditional

我使用Struts2开发了一个简单的Web应用程序。 我在数据库中进行插入和更新,执行此操作后,它将重定向到公共jsp页面。 我想只显示我的jsp页面基础的特定div,在该基础上执行了DML操作。

代码:

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="default" extends="struts-default">

    <action name="insert" class="student.InsertDao">
       <result name="success">/Trigger/new.jsp</result>
    </action>
    <action name="fetchdetails" class="student.FetchDetails">
       <result name="success">/Trigger/fetchDetails.jsp</result>
    </action>
    <action name="update" class="student.UpdateDao">
       <result name="success">/Trigger/updated.jsp</result>
    </action>

   </package>
   </struts>

Updated.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>Details</title>
</head>
<body>
<h1>Success</h1>
<div id="1">
    <table align="center" border="1">
        <tr>
            <th>ID</th>
            <th>Student Name</th>
            <th>Password</th>
            <th>Email</th>
        </tr>
        <s:iterator value="list">
            <tr>
                <td align="center"><s:property value="id" /></td>
                <td align="center"><s:property value="sname" /></td>
                <td align="center"><s:property value="password" /></td>
                <td align="center"><s:property value="email" /></td>
            </tr>
        </s:iterator>
    </table>
  </div>
   <div id="2">
    <!-- code here -->
   </div>
   <div id="3">
    <!-- code here -->
   </div>

  </body>
  </html>

以下是调用“更新”操作的文件。 getIDforUpdate.jsp

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
   <%@ taglib uri="/struts-tags" prefix="s"%>
   <!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>Details</title>
    </head>
    <body>
    <h3>Details:</h3>
     <s:form action="update">
      <s:iterator value="list">
        <s:label value="ID:" />
        <s:textfield name="bean.id" value="%{id}" readonly="true">  </s:textfield>
        <br>
        <s:label value="Student's Name:" />
        <s:textfield name="bean.sname" value="%{sname}"></s:textfield>
        <br>
        <s:label value="Email:" />
        <s:textfield name="bean.email" value="%{email}"></s:textfield>
        <br>
        <s:label value="Password:" />
        <s:textfield name="bean.password" value="%{password}"></s:textfield>
        <br>
        <s:submit name="submit" value="Apply Changes" />
        </s:iterator>
        </s:form>
        </body>
        </html>

UpdateDao.java

package student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.ConnectionProvider;

import student.Student;

import com.opensymphony.xwork2.ActionSupport;

 public class UpdateDao extends ActionSupport {
 Student bean;

 public Student getBean() {
    return bean;
  }

   public void setBean(Student bean) {
    this.bean = bean;
  }

  ArrayList<Student> list = new ArrayList<Student>();

   public ArrayList<Student> getList() {
    return list;
   }

  public void setList(ArrayList<Student> list) {
    this.list = list;
  }

  int status = 0;

   public String execute() {
    UpdateDao upd = new UpdateDao();

    status = upd.validate(bean);
    try {
        Connection con = ConnectionProvider.getCon();
        PreparedStatement ps = con
                .prepareStatement("select *from student where id like ?");
        ps.setString(1, bean.getId());
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            Student std = new Student();
            std.setId(rs.getString(1));
            std.setSname(rs.getString(2));
            std.setPassword(rs.getString(3));
            std.setEmail(rs.getString(4));
            list.add(std);
        }
        } catch (Exception e) {
        System.out.println(e);
        }

      if (status > 0)
        return SUCCESS;
      else
        return ERROR;
      }

     public static int validate(Student bean) {
     UpdateDao u = new UpdateDao();
     try {

        Connection con = ConnectionProvider.getCon();
        // System.out.println("connection established");
        PreparedStatement ps = con
                .prepareStatement("Update student set   sname=?,password=?,email=? where id like ?");
        // System.out.println("query");

        ps.setString(1, bean.getSname());
        ps.setString(2, bean.getPassword());
        ps.setString(3, bean.getEmail());
        ps.setString(4, bean.getId());
        System.out.println("ps=" + ps);

        u.status = ps.executeUpdate();
        System.out.println("Status:" + u.status);
        } catch (Exception e) {
        System.out.println(e);
       }
        return u.status;
       }
       }

从这个javas类中,结果被识别,struts将重定向到upddated.jsp页面 现在,如果已经调用了“insert”动作,那么我想显示id为2的div,如果调用了“update”动作,那么我想显示id为1的div。

我怎样才能实现这一点,因为它重定向fronm struts?

0 个答案:

没有答案