首先是做一个struts例子,里面有几个action类,怎么对应的访问资源(如 http://pcName:8080/J2EE01/Login.do)都不可用,struts-config.xml文件确认已经读取了,这个是什么原因? 困扰几天了都搞不定?郁闷之极...求大伙帮看是什么原因?下面分别是各个文件(注意:这个大小写之类的问题我已经注意;奇怪我另建立一个简单的project来测试,只有一个Login action,对应的访问资源/Login.do 又可以成功访问)-------------------------------
web.xml
-------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <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>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</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.htm</welcome-file>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>login.jsp</welcome-file>
  </welcome-file-list> 
  <jsp-config>
   <taglib>
   <taglib-uri>http://www.mysite.com/tags/struts-html</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
   </taglib>
   <taglib>
   <taglib-uri>http://www.mysite.com/tags/struts-logic</taglib-uri>
   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
   </taglib>
   <taglib>
   <taglib-uri>http://www.mysite.com/tags/struts-bean</taglib-uri>
   <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
   </taglib>
   </jsp-config>
</web-app>
------------------------------
struts-config.xml
------------------------------<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
  <data-sources>
    <data-source key="org.apache.struts.action.DATA_SOURCE" type="org.apache.struts.util.GenericDataSource">
      <set-property property="password" value="12345" />
      <set-property property="minCount" value="1" />
      <set-property property="maxCount" value="100" />
      <set-property property="user" value="sa" />
      <set-property property="driverClass" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
      <set-property property="description" value="Connection to MS SQL 2000 DB" />
      <set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=TESTDBEmployee" />
      <set-property property="readOnly" value="false" />
      <set-property property="autoCommit" value="true" />
      <set-property property="loginTimeout" value="10000" />
    </data-source> 
  </data-sources>
  
  <form-beans>
    <form-bean name="loginForm" type="com.employee.struts.form.LoginForm" />
  </form-beans>  <global-exceptions />
  <global-forwards>
    <forward name="login" path="/login.jsp" />    
  </global-forwards>  <action-mappings> 
    <action      
      attribute="loginForm" 
      path="/Login"
      type="com.employee.struts.action.LoginAction"   
      name="loginForm"         
      input="/login.jsp"      
      scope="request">
      <forward name="success" path="/pages/EmployeeList.jsp"/>
    </action> 
    <action 
      path="/EmployeeList" 
      type="com.employee.struts.action.EmployeeListAction" 
      scope="request" 
      input="/pages/noData.jsp">     
      <forward name="success" path="/pages/employeeList.jsp"/>
    </action> 
  </action-mappings>   <message-resources parameter="com.employee.struts.ApplicationResources" />
</struts-config>
---------------------------
login.jsp
---------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%
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>MVC模式, Struts V1.1 框架的应用 - <bean:message key="app.title"/></title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="webstyle/style01.css">  </head>
  
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td width=75%><b>MVC模式, Struts V1.1 应用[Simple Employee Manage System]</b></td>
<td width=25% align=right><a class=a02 href="./">Back</a></td>
</tr> 
</table> 
<hr size=1 color=red> <html:errors/><html:form action="/Login" focus="username" method="post"> 
<table width="100%" border="0">
<tr>
<td width="45%"><bean:message key="app.username"/>: </td>
<td width="55%"><html:text value="username" property="username"></html:text></td>
</tr>  
<tr>
<td><bean:message key="app.password"/>: </td>
<td><html:password redisplay="true" property="password"></html:password></td>
</tr>
<tr>  
<td>&nbsp;</td> 
<td>&nbsp;<html:submit value="Submit" property="submit"></html:submit></td>
</tr>
</table>
</html:form> 
<br>
<br>
</body>
</html>

解决方案 »

  1.   


    ----------------------------
    loginAction.java
    ----------------------------
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.employee.struts.action;//---------------------------------------------
    //import about System package
    //---------------------------------------------
    import java.io.IOException;
    //---------------------------------------------
    //import about servlet package
    //---------------------------------------------
    import javax.servlet.http.*;  //import more, key package
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.*;   //import more, key package
    import org.apache.struts.action.*;   //import more, key package
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import javax.sql.DataSource;
    //import java.sql.*;    //import more, key package
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.SQLException;import com.employee.struts.*;  //import java bean package
    import com.employee.struts.form.*;     //import ActionForm Bean package/** 
     * MyEclipse Struts
     * Creation date: 03-25-2011
     * 
     * XDoclet definition:
     * @struts.action input="/login.jsp" scope="request" validate="true"
     */
    public class LoginAction extends Action {
    /*
     * Generated Methods
     */
    //-----------------------------------
    protected String getUser(String username, String password){
    //Function to check and get the USER with current username and password
    String user=null;
    Connection DBConn=null;
    Statement stmt=null;
    ResultSet rs=null;
    String xSQL="";
    ServletContext context=servlet.getServletContext();
    //DataSource dataSource=(DataSource)context.getAttribute(org.apache.struts.Globals.DATA_SOURCE_KEY);
    DataSource dataSource=(DataSource)context.getAttribute(Globals.DATA_SOURCE_KEY);
    try{
    DBConn=dataSource.getConnection();
    stmt=DBConn.createStatement();
    xSQL="select * from Employees where username ='" +username+ "' and password ='" + password + "'";
    rs=stmt.executeQuery(xSQL);
    if(rs.next()){
    user=rs.getString("username");
    }else{
    System.err.println("------ User not found ------");
    }

    }catch(SQLException e){
    System.err.println("Exception: "+e.getMessage());
    }finally{
    if(rs!=null){
    try{
    rs.close();
    }catch(SQLException sqle){
    System.err.println("Exception RS : "+sqle.getMessage());
    }
    }
    rs=null;

    if(stmt!=null){
    try{
    stmt.close();
    }catch(SQLException sqle){
    System.err.println("Exception stmt : "+sqle.getMessage());
    }
    }
    stmt=null;

    if(DBConn!=null){
    try{
    DBConn.close();
    }catch(SQLException sqle){
    System.err.println("Exception DBConn : "+sqle.getMessage());
    }
    }
    DBConn=null;
    }
    return user;
    }

    //-----------------------------
    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
    // TODO Auto-generated method stub
    String user=null;
    String target="success";

    String username=((LoginForm)form).getUsername();
    String password=((LoginForm)form).getPassword();
    user=getUser(username,password);
    if(user==null){
    target="login";
    ActionErrors errors=new ActionErrors();
    errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("errors.login.unknown",username));  
    if(!errors.isEmpty()){
    saveErrors(request,errors);
    }
    }else{
    HttpSession session=request.getSession();
    session.setAttribute("USER",user);
    }

    return (mapping.findForward(target)); 
    }
    }
      

  2.   

    struts-config.xml文件确认已经读取了,那可能是路径问题,试着访问:http://pcName:8080/J2EE01/Login/Login.do
      

  3.   

    2L 说的那个情况,应该不是今天新发现:struts 1.1版本中,配置文件 struts-config.xml 中的为什么 data-source元素包含 set-property元素时系统不能正常读取该配置文件,其中 set-property 元素是使用MyEclipse生成,应该不成问题吧?发现其它元素包含 set-property 元素时也struts系统也不能正常读取该配置文件?以下红色部分去掉就可以正常读取了,
    如果不能使用 set-property 那也就是说不能使用struts的数据库连接池,(意味着DB连接得重写而不能使用struts现成)
    这个是什么原因? 如何使用struts的数据库连接池呢?求高手帮解答
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
    <struts-config>
      <data-sources>
        <data-source key="org.apache.struts.action.DATA_SOURCE" type="org.apache.struts.util.GenericDataSource">
          <set-property property="password" value="12345" />
          <set-property property="minCount" value="1" />
          <set-property property="maxCount" value="100" />
          <set-property property="user" value="sa" />
          <set-property property="driverClass" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
          <set-property property="description" value="Connection to MS SQL 2000 DB" />
          <set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=TESTDBEmployee" />
          <set-property property="readOnly" value="false" />
          <set-property property="autoCommit" value="true" />
          <set-property property="loginTimeout" value="10000" />

        </data-source> 
      </data-sources>
      
      <form-beans>
        <form-bean name="loginForm" type="com.employee.struts.form.LoginForm" />
      </form-beans>  <global-exceptions />
      <global-forwards>
        <forward name="login" path="/login.jsp" />    
      </global-forwards>  <action-mappings> 
        <action      
          attribute="loginForm" 
          path="/Login"
          type="com.employee.struts.action.LoginAction"   
          name="loginForm"         
          input="/login.jsp"      
          scope="request">
          <forward name="success" path="/pages/EmployeeList.jsp"/>
        </action> 
        <action 
          path="/EmployeeList" 
          type="com.employee.struts.action.EmployeeListAction" 
          scope="request" 
          input="/pages/noData.jsp">     
          <forward name="success" path="/pages/employeeList.jsp"/>
        </action> 
      </action-mappings>   <message-resources parameter="com.employee.struts.ApplicationResources" />
    </struts-config>
    还有个问题今晚跑去书店大致看了一下 struts 2 冒似相对简单,那 Eclipse 3.2.0版本 MyEclipse 5.7版本 可以用来建立 struts 2 的环境吗? ,请高手帮看看....
      

  4.   

    还有个问题今晚跑去书店大致看了一下 struts 2 冒似相对简单,那 Eclipse 3.2.0版本 MyEclipse 5.1版本 可以用来建立 struts 2 的环境吗? ,请高手帮看看....
    struts 1.1版本中,配置文件 struts-config.xml 中的为什么 data-source 和action等元素含有 set-property子元素时系统就不能读取该struts配置文件,其中 set-property 元素是使用MyEclipse 开发环境生成,应该不成问题?但为什么就不能读取呢?请高手帮看看....