Alex刺客

Dancing fingers, damage world. -- 舞动手指,破坏世界.

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  57 随笔 :: 0 文章 :: 76 评论 :: 0 Trackbacks
唉我的第一篇日志真难发布,内容刚刚写完,不小心点了一个X火狐关了就没了!
一、在下载的struts2.1.6-all.zip找到以下包放入项目中WEB-INF/lib/下;

1.commons-fileupload-1.2.1.jar   <--文件上传
2.commons-logging-1.0.4.jar   <--日志
3.freemarker-2.3.13.jar  <--模板引擎
4.ognl-2.6.11.jar <--Object-Graph Navigation Language
5.struts2-core-2.1.6.jar   <--struts2核心
6.xwork-2.1.2.jar  <--xwork核心

二、在项目的web.xml文件中添加配置信息
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>Struts2HelloWord</display-name>
 4   
 5   <!--过滤器 -->
 6   <filter>
 7   
 8       <!-- 指定这个过滤的名字 -->
 9       <filter-name>struts2</filter-name>
10       
11       <!-- 指定这个过滤器的类路径. 这个类是'struts2-core-2.1.6.jar'核心过滤分配器 -->
12       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
13   </filter>
14   
15   <!--过滤器映射 -->
16   <filter-mapping>
17       
18       <!-- 指定当前所有请求至struts2过滤器 -->
19       <filter-name>struts2</filter-name>
20       <!-- 通配符*代表所有 -->
21       <url-pattern>/*</url-pattern>
22   </filter-mapping>
23   
24   <!-- 指定访问当前项目的主页 -->
25   <welcome-file-list>
26     <welcome-file>index.jsp</welcome-file>
27   </welcome-file-list>
28 </web-app>

三、新建一个名为HelloAction的Action类(此类在helloworld包中)
 1 package helloworld;
 2 
 3 public class HelloAction {
 4     
 5     //用于保存用户名
 6     private String userName;
 7     //用于设置给welcome.jsp的信息
 8     private String massage;
 9     
10     //setter getter方法
11     public void setUserName(String userName){
12         this.userName = userName;
13     }
14     
15     public String getUserName(){
16         return userName;
17     }
18     
19     public void setMassage(String massage){
20         this.massage = massage;
21     }
22     
23     public String getMassage(){
24         return massage;
25     }
26     
27     //默认处理用户请求的方法
28     public String execute() throws Exception{
29         
30         //判断用户名不能为空白
31         if(!getUserName().equals("")){
32             setMassage("scorpion剌客:"+getUserName()+"欢迎来到struts2的世界!");
33             //在控制台输出massage成员
34             System.out.println(getMassage());
35             
36             //返回success字符串
37             return "success";
38         }else{
39             
40             //返回error字符串
41             return "error";
42         }
43     }
44 }

四、在src中新建一个struts.xml文件(如果你用文本编辑器struts.xml应在项目/WEB-INF/classes下)
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!--定义当前xml文档为struts2配置文档  -->
 4 <!DOCTYPE struts PUBLIC
 5     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 6     "http://struts.apache.org/dtds/struts-2.1.dtd">
 7     <!-- 开始配置struts2 -->
 8 <struts>
 9     <!-- 定义一个helloworld包,在此包的所有类都继承struts-default包(此包在struts2-core-2.1.6.jar/struts-default.xml里)-->
10     <package name="helloworld" extends="struts-default">
11     <!-- 定义一个名为hello的Action 指定类文件为helloworld.HelloAction -->
12         <action name="hello" class="helloworld.HelloAction">
13         <!-- 当helloworld.HelloAction类的execute方法返回success字符串给struts2框架时,执行welcome.jsp -->
14         <!-- 以下默认为<result name="success">……-->
15             <result>/welcome.jsp</result>
16         <!-- 当helloworld.HelloAction类的execute方法返回error字符串给struts2框架时,执行error.jsp -->
17             <result name="error">/error.jsp</result>
18         </action>
19     </package>
20 </struts>

五、新建一个index.jsp
 1 <%@ page language="java"  contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
 2 <%--struts2标签库配置--%>
 3 <%@ taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5 <html>
 6 <head>
 7 <title>欢迎</title>
 8 </head>
 9 <body>
10     <%--向客户端生成一个form表单 --%>
11     <s:form action="hello">
12         <%-- 生成一个text文本--%>
13         <s:textfield name="userName" label="请问你的名字是"/>
14         <%-- 生成一个提交按钮--%>
15         <s:submit value="告诉他"/>
16     </s:form>
17 </body>
18 </html>

六、新建一个welcome.jsp
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 <html xmlns="http://www.w3.org/1999/xhtml">
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 6 <title>welcome</title>
 7 </head>
 8 <body>
 9     
10     ${requestScope.massage}
11 </body>
12 </html>

七、新建一个error.jsp
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 <html xmlns="http://www.w3.org/1999/xhtml">
 4 <head>
 5 <title></title>
 6 </head>
 7 <body>
 8     您不可能没有名字!(偷偷告诉我可以啦!^_^)
 9 </body>
10 </html>

八、编译 部署 访问
      
    这是我在http://www.blogjava.net/ 上第一篇日志希望能够认识更多的朋友!

                                                                                                
posted on 2009-07-01 21:16 Alex刺客 阅读(475) 评论(0)  编辑  收藏 所属分类: Struts2 Study Notes

只有注册用户登录后才能发表评论。


网站导航: