2

Eclipse で小さな「動的 Web プロジェクト」を作成し、データベース オブジェクトを表示/編集/作成および削除しようとしました。Tomcat サーバーが実行されます。しかし、HTTP ステータス 404 エラーが発生します: 要求されたリソース (/TestDynWeb/) は利用できません (http://localhost:8010/TestDynWeb)。すべてのコード ファイルを表示したいと思います。たぶん、あなたは何が悪いのか考えているでしょう。

ウェブコンテンツ/WEB-INF:

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
<display-name>TestDynWeb</display-name>

 <servlet>
  <servlet-name>action</servlet-name>
 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

 <jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
   <taglib-location>/WebContent/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd" >

<struts-config>
    <!-- Form Bean Definitions -->
    <form-beans>
        <form-bean name="ListForm" type="list.forms.ListForm"/>
    </form-beans>

    <!--  Action Mapping Definitions  -->
    <action-mappings>
        <action path="/showlist" type="list.misc.ShowList" validate="false" scope="session">
        <forward name="success" path="/jsp/listScreen.jsp"/>
    </action>

    <!-- Example of Struts Dispatch Action : has the extra attribute parameter-->
    <action path="/listaction" type="list.actions.ListAction" parameter="actionMethod" name="ListForm"
     validate="false" scope="session">
        <forward name="addObject" path="/jsp/createScreen.jsp"/>
        <forward name="editObject" path="/jsp/editScreen.jsp"/>
    </action>
    </action-mappings>
</struts-config>

ウェブコンテンツ/jsp:

listScreen.jsp

<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Iterator"%>
<html>
<body>
    <b>Available Data Base Objects</b>
    <form name="listform" action="/listaction.do">
        <table style="background-color: #82CAFA;">
            <tr style="color: white;">
                <th>&nbsp;</th>
                <th>Object ID</th>
                <th>Object Name</th>
                <th>Person ID</th>
                <th>Date (Dev)</th>
                <th>Version (Dev)</th>
            </tr>
            <%
                List objectsList = (ArrayList) request.getAttribute("objectsList");
                Iterator itr = objectsList.iterator();
                while (itr.hasNext()) {
                    Map map = (HashMap) itr.next();
            %>
            <tr>
                <td><input type="radio" name="dboId"
                    value='<%=map.get("dboId")%>'
                    onclick="javascript:enableEditDelete();"></td>
                <td><%=map.get("dboID")%></td>
                <td><%=map.get("dboDevName")%></td>
                <td><%=map.get("personId")%></td>
                <td><%=map.get("changedByTimestampDEV")%></td>
                <td><%=map.get("releaseVersionDev")%></td>
            </tr>
            <%
                }
            %>
        </table>
        </p>
        <p>
        <table>
            <tr>
                <td><input type="submit" name="actionMethod" id="editbutton"
                    value="Edit Object" disabled="true" style="color: black;" /></td>
                <td><input type="submit" name="actionMethod" id="deletebutton"
                    value="Delete Object" disabled="true" onclick="return checkDelete();" style="color: black;" /></td>
            </tr>
        </table>
    </form>
    </p>
    <script>
        function checkDelete() {
            return confirm("Are u sure to delete this object..?");
        }
        function enableEditDelete() {
            document.getElementById('editbutton').disabled = false;
            document.getElementById('deletebutton').disabled = false;
        }
    </script>
</body>
</html>

editScreen.jsp

<b>Edit Object</b>
<html:form>
    <p>
    </p><table style="background-color: #82CAFA;">
        <tr style="color: white;">
            <td>Object ID</td>
            <td><html:text property="dboId" disabled="true"/></td>
        </tr>
        <tr style="color: white;">
            <td>Object Name</td>
            <td><html:text property="dboDevName"/></td>
        </tr>
        <tr style="color: white;">
            <td>Person ID</td>
            <td><html:text property="personId"/></td>
        </tr>
        <tr style="color: white;">
            <td>Date (Dev)</td>
            <td><html:text property="changedByTimestampDEV"/></td>
        </tr>
        <tr style="color: white;">
            <td>Version (Dev</td>
            <td><html:text property="releaseVersionDev"/></td>
        </tr>
    </table>
    <p/>
    <p>
    </p><table>
        <tr>
            <td><input type="submit" name="actionMethod" value="Save Object" style="color: black;"/></td>
        </tr>
    </table>
</html:form>
<p/>

createScreen.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<body>

    <b>Add Object</b>
    <html:form>
        <table style="background-color: #82CAFA;">
            <tr>
                <td>Object ID</td>
                <td><html:text property="dboId" disabled="true" /></td>
            </tr>
            <tr>
                <td>Object Name</td>
                <td><html:text property="dboDevName" /></td>
            </tr>
            <tr>
                <td>Person ID</td>
                <td><html:text property="personId" /></td>
            </tr>
            <tr>
                <td>Date (Dev)</td>
                <td><html:text property="changedByTimestampDEV" /></td>
            </tr>
            <tr>
                <td>Version (Dev</td>
                <td><html:text property="releaseVersionDev" /></td>
            </tr>
        </table>
        </p>
        <p>
        <table>
            <tr>
                <td><input type="submit" name="actionMethod" value="Add Object" /></td>
            </tr>
        </table>
    </html:form>
    </p>
</body>
</html>

/config:

appContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="url"
            value="jdbc:jtds:sqlserver://localhost:1433;databaseName=NARTrackingDB" />
        <property name="username" value="abc" />
        <property name="password" value="xyz" />            
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="objDao" class="tracking.dao.DboDao">
        <property name="transactionManager" ref="transactionManager" />
    </bean>

</beans> 

DAO クラス:

package tracking.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import javax.sql.DataSource;

import org.objectweb.asm.Type;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import tracking.bean.DboBean;

public class DboDao {
    private DataSourceTransactionManager transactionManager;
    private JdbcTemplate jdbcTemplate;
    private DataSource dataSource;  // either use @Autowire or provide xml config 

    //private String dboDevName;
    public DboDao() {
        // TODO Auto-generated constructor stub
        super();
    }

    public DboBean read(final int dboId) {//String dboId
        // TODO Auto-generated method stub
        String sql = "SELECT * FROM da_tracking " 
                        + "WHERE ins_ID = " + dboId;
        final Object[] params = new Object[] {};
        //Creating object being queried
        final DboBean dboBean = new DboBean();
        //Process query Results
        jdbcTemplate.query(sql, params, new RowCallbackHandler(){
            public void processRow(ResultSet rs) throws SQLException{
                dboBean.setDboId(rs.getString("ins_ID"));
                dboBean.setDboDevName(rs.getString("ins_name"));
            }           
        });
        return dboBean;//returns queried objects
    }

    public int save(DboBean record ) {
        // TODO Auto-generated method stub
    String sql = "UPDATE da_tracking" 
                + " SET ins_name= ?,"  
                + " ins_dev_scripted = ?" 
                + " WHERE ins_ID = ?" ;
    Object[] params = new Object[] { 
            record.getDboDevName(), 
            record.getDevScripted(), 
            record.getDboId()
    }; 
    int[] types = new int[]{
            Types.VARCHAR, 
            Types.BIT, 
            Types.INTEGER}; 
    return jdbcTemplate.update(sql, params, types); 

    }

    public int createInstance(DboBean record) {
        // TODO Auto-generated method stub
        String sql = "INSERT INTO da_tracking" 
                + "(ins_ID,ins_name,ins_deployed_to_uat, ins_dev_status,ins_dev_scripted,ins_dev_changed_by_timestamp"
                + "change_details,db_owner_ID,type_ID,person_ID,changeType_ID,ins_uat_changed_by_timestamp,release_version_dev"
                + "release_version_uat) VALUES"
                + "( ?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        Object[] params = new Object[] { 
            record.getDboId(),record.getDboDevName(),record.getDeployedToUAT(),record.getDevStatus(),record.getDevScripted(), 
            record.getChangedByTimestampDEV(),record.getChangeDetails(),record.getDbOwner(),record.getTypeId(),record.getPersonId(),
            record.getChangedByTimestampUAT(),record.getReleaseVersionDev(),record.getReleaseVersionUat()
        }; 

        int[] types = new int[]{
            Types.INTEGER, Types.VARCHAR, Types.BIT, Types.INTEGER, Types.BIT, 
            Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR
            }; 
        return jdbcTemplate.update(sql, params, types); 
    }

    public void delete(String dboId) {
        // TODO Auto-generated method stub
        String sql = "DELETE FROM da_tracking "
                + "WHERE ins_ID = " + dboId;
        Object[] params = new Object[] {};
        //Process query Results
        jdbcTemplate.update(sql, params);       
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    //Spring setter injector used
    public void setTransactionManager(DataSourceTransactionManager transactionManager){
        this.transactionManager = transactionManager;
        DataSource datatSource = transactionManager.getDataSource();  
        jdbcTemplate = new JdbcTemplate(datatSource); 
    }

}

--DBOBean.java には getter と setter が含まれています -- ここに表示する必要はありません

ShowList.java:

package list.misc;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowList extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Show Objects List");
        ListObjects l = ListObjects.getInstance();
        request.setAttribute("objectsList", l.getObjectList());
        return mapping.findForward("success");
    }
}

リストオブジェクト:

package list.misc;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

public class ListObjects {

    int objectIdCount = 1000;
    Map<Integer, StoreObject> objectMap = new HashMap<Integer, StoreObject>();
    private static ListObjects objects = null;

    private ListObjects() {
    }

    public static ListObjects getInstance() {
        if (objects == null) {
            objects = new ListObjects();
        }
        return objects;
    }

    public void storeObject(String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
        StoreObject so = new StoreObject();
        objectIdCount++;
        so.addObject(objectIdCount, dboDevName, personId, changedByTimestampDEV, releaseVersionDev);
        objectMap.put(objectIdCount, so);
    }

    public void updateObject(int dboId, String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
        StoreObject so = objectMap.get(dboId);
        so.updateObject(dboId, dboDevName, personId, changedByTimestampDEV, releaseVersionDev );
    }


      public Map searchObject(int dboId) { 
          return objectMap.get(dboId).getObject(); 
      }


    public void deleteObject(int dboIds) {
        objectMap.remove(dboIds);
    }

    // Inner Class used to persist the app data ie. object details.
    public class StoreObject {

        private String dboDevName;
        private String changeDetails;
        private String changedByTimestampDEV;
        String releaseVersionDev;
        private int personId;
        private int objectIdCount;
        private int dboId;

        StoreObject() {
        }

        public void addObject(int objectIdCount, String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
            this.objectIdCount = objectIdCount;
            this.dboDevName = dboDevName;
            this.personId = personId;
            this.changedByTimestampDEV = changedByTimestampDEV;
            this.releaseVersionDev = releaseVersionDev;
        }

        public void updateObject(int dboId, String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
            this.dboId = dboId;
            this.dboDevName = dboDevName;
            this.personId = personId;
            this.changedByTimestampDEV = changedByTimestampDEV;
            this.releaseVersionDev = releaseVersionDev;
        }

        @SuppressWarnings("unchecked")
        public Map getObject() {
            Map objects = new HashMap();
            objects.put("objectIdCount", this.objectIdCount);
            objects.put("dboDevName", this.dboDevName);
            objects.put("personId", this.personId);
            objects.put("changedByTimestampDEV", this.changedByTimestampDEV);
            objects.put("releaseVersionDev", this.releaseVersionDev);
            return objects;
        }
    }

    public List getObjectList() {
        List objectsList = new ArrayList();
        Set s = objectMap.keySet();
        Iterator itr = s.iterator();
        while (itr.hasNext()) {
            objectsList.add(objectMap.get((Integer) itr.next()).getObject());
        }
        return objectsList;
    }
}

--ListForm.java にはゲッターとセッターが含まれています -- ここでは必要ありません。

ListAction.java:

package list.actions;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Map;

import list.forms.ListForm;
import list.misc.ListObjects;

public class ListAction extends DispatchAction {
    public ActionForward AddObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Add Object Page");
        return mapping.findForward("addObj");
    }

    public ActionForward EditObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Edit Object Page");
        int dboId = Integer.parseInt(request.getParameter("dboId"));

        ListObjects l = ListObjects.getInstance();
        @SuppressWarnings("rawtypes")
        Map objMap = l.searchObject(dboId);

        // Used form bean class methods to fill the form input elements with
        // selected object values.
        ListForm lf = (ListForm) form;

        lf.setDboId((Integer) objMap.get("dboId"));
        lf.setDboDevName(objMap.get("dboDevName").toString());
        lf.setTypeId((Integer) objMap.get("typeId"));
        lf.setChangeDetails(objMap.get("changeDetails").toString());
        lf.setChangeType((Integer) objMap.get("changeType"));
        lf.setDevScripted((Boolean) objMap.get("devScripted"));
        lf.setPersonId((Integer) objMap.get("personId"));
        lf.setChangedByTimestampDEV(objMap.get("changedByTimestampDEV").toString());
        lf.setChangedByTimestampUAT(objMap.get("changedByTimestampUAT").toString());
        lf.setReleaseVersionDev(objMap.get("releaseVersionDev").toString());
        lf.setReleaseVersionUat(objMap.get("releaseVersionUat").toString());

        return mapping.findForward("addObject");
    }

    public ActionForward SaveObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Save Object");
        // Used form bean class methods to get the value of form input elements.
        ListForm lf = (ListForm) form;
        int dbOwner = lf.getDbOwner();
        String dboDevName = lf.getDboDevName();
        int typeId = lf.getTypeId();
        String changeDetails = lf.getChangeDetails();
        int changeType = lf.getChangeType();
        Boolean devScripted = lf.isDevScripted();
        int personId = lf.getPersonId();
        String changedByTimestampDEV = lf.getChangedByTimestampDEV();
        String changedByTimestampUAT = lf.getChangedByTimestampUAT();
        String releaseVersionDev = lf.getReleaseVersionDev();
        String releaseVersionUat = lf.getReleaseVersionUat();

        ListObjects l = ListObjects.getInstance();
        l.storeObject(dboDevName, personId, changedByTimestampDEV, releaseVersionDev);
        return new ActionForward("/showlist.do", true);
    }

    public ActionForward CreateObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Update Object");
        ListForm lf = (ListForm) form;
        int dboId = lf.getDboId();
        String dboDevName = lf.getDboDevName();
        int typeId = lf.getTypeId();
        String changeDetails = lf.getChangeDetails();
        int changeType = lf.getChangeType();
        Boolean devScripted = lf.isDevScripted();
        int personId = lf.getPersonId();
        String changedByTimestampDEV = lf.getChangedByTimestampDEV();
        String changedByTimestampUAT = lf.getChangedByTimestampUAT();
        String releaseVersionDev = lf.getReleaseVersionDev();
        String releaseVersionUat = lf.getReleaseVersionUat();

        ListObjects l = ListObjects.getInstance();
        l.updateObject(dboId, dboDevName, personId, changedByTimestampDEV, releaseVersionDev);
        return new ActionForward("/showlist.do", true);
    }

    public ActionForward DeleteObject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Delete Object");
        int dboId = Integer.parseInt(request.getParameter("dboId"));
        ListObjects lo = ListObjects.getInstance();
        lo.deleteObject(dboId);
        return new ActionForward("/showlist.do", true);
    }
}

あらゆる助けを前もって感謝します。

4

3 に答える 3

1

おそらく、Tomcatサーバーにアプリケーションをデプロイしていません。サーバービューでサーバーを右クリックし、[追加と削除]を押して、プロジェクトをサーバーに追加する必要があります。

于 2012-09-16T14:42:09.553 に答える
1

プロジェクトに移動し、右クリックして展開アセンブリを確認します。web.xml を含むフォルダーが正しく含まれているかどうかを確認します。別のフォルダーに web.xml を構成し、配置アセンブリに Webcontent フォルダーを追加した可能性があります。

于 2013-08-04T08:15:55.843 に答える
0

「Deployment Assemply」に WebContent フォルダーがあることを確認します。そうでない場合は、追加します。

于 2012-09-17T11:55:39.723 に答える