struts配置。泪奔...

       

     说多了都是泪啊,配置一个环境一天才搞定。不错the requested resource (/login) is not available in struts,就是找不到什么什么class.亦或there is no action mapped for namespace / and action name。还有编码错误。反正是啥错误都有啊。各种百度,各种google.终于算是配置成功了。

       第一步:下载struts-2.3.15.1。全部下吧,里面有包,有文档,有示例。我们需要的开发包在Lib文件夹里面。

 

       第二步:第一步算是准备了材料,材料完了之后需要炒菜了,这就需要点耐心外加一定技巧了。首先在myeclipse里面新建一个web项目,既然是用struts开发,那么肯定少不了要添加很多的包,这样才能引用别人的成果不是?引用的包主要有(当然,不同版本可能会有一两个包不一样):commons-fileupload-1.3.jar、commons-io-2.0.1.jar、commons-lang3-3.1.jar、commons-logging-1.1.3.jar、commons-logging-api-1.1.jar、freemarker-2.3.19.jar、javassist-3.11.0.GA.jar、ognl-3.0.6.jar、struts2-core-2.3.15.1.jar、xwork-core-2.3.15.1.jar。

 

      第三步:引入包之后,你就可以写jsp页面,web.xml和struts2.xml配置文件了。

      先讲struts2的配置文件吧。一定记得struts2怎么拼写的,还有一些拼写问题,我也遇到了。总之,一点都不能错。struts2文件写好了之后,放在WebRoot\WEB-INF\classes文件夹里面。这个问题会害死你的,如果不注意。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
 3 
 4    <struts>
 5    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
 6       <package name="strut" extends="struts-default">
 7          <action name="login" class="com.LoginAction">
 8            <result name="success">/success.jsp</result>
 9          </action>
10       </package>
11    </struts>

 

 

    然后是web.xml的配置。注意其中的filter-class是:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。这个问题没搞懂,估计你会发现无数个error.can not find...class.全出来了。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <welcome-file-list>
 8     <welcome-file>index.jsp</welcome-file>
 9   </welcome-file-list>
10   <filter>
11         <filter-name>struts2</filter-name>
12         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13    </filter>
14 
15     <filter-mapping>
16         <filter-name>struts2</filter-name>
17         <url-pattern>/*</url-pattern>
18     </filter-mapping>
19 </web-app>

 

      配置文件搞定了,下面就轮到jsp页面了。我写了一个简单的页面。注意编码我已经改成pageEncoding="UTF-8"。否则可能会出现乱码的问题。你改成gb2312也是乱码,因为上面的配置文件貌似都是utf-8.

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="/struts-tags" prefix="s" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25     This is my JSP page. <br>
26     <s:form action="login" method="post">
27     <s:label value="系统登陆"></s:label>
28     <s:textfield name="username" label="账号" />
29     <s:password name="password" label="密码" />
30     <s:submit value="登录" />
31    </s:form>
32   </body>
33 </html>

 

   对了,还有一个响应class.他是LoginAction。我写的很简单,就是执行一个方法而已。

 1 package com;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 @SuppressWarnings("serial")
 5 public class LoginAction extends ActionSupport {
 6     
 7     public String excute() throws Exception{
 8             return "success";
 9     }
10 }

 

     当然记得新建一个success.jsp的页面喔,否则成功后转到哪里去呢?

   现在觉得貌似很简单。当时怎么那么多错误呢?催。。。Mark一下、sleepping.. 

posted @ 2013-08-21 00:49  白来了123  阅读(251)  评论(4编辑  收藏  举报