`
lyx_2709
  • 浏览: 314008 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

struts2初体验(附视频)

阅读更多

本文主要讲述struts2的基本配置,注意是基本配置,struts2的配置不仅仅是这些。主要是为了演示第一个程序,所以本人认为越简单越好。

大多的配置基本和webwork相同。如果你会webwork只需要你5分钟的时间。

Web.xml文件添加代码如下:

xml 代码
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.     <filter>  
  8.         <filter-name>struts<!---->filter-name>  
  9.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher<!---->filter-class>  
  10.     <!---->filter>  
  11. <filter-mapping>  
  12.         <filter-name>struts<!---->filter-name>  
  13.         <url-pattern>/*<!---->url-pattern>  
  14.     <!---->filter-mapping>  
  15. <!---->web-app>  

建一个index.jsp页面如下:

<%@ page contentType="text/html; charset=GBK"%><o:p></o:p>

<%@ taglib prefix="s" uri="/struts-tags"%><o:p></o:p>

<html><o:p></o:p>

    <head><o:p></o:p>

       <title>index<!---->title><o:p></o:p>

    <!---->head><o:p></o:p>

    <body><o:p></o:p>

       <s:form action="index"><o:p></o:p>

           <s:textfield name="userName" label="enter your name"><!---->s:textfield><o:p></o:p>

           <s:submit /><o:p></o:p>

       <!---->s:form><o:p></o:p>

    <!---->body><o:p></o:p>

<!---->html><o:p></o:p>

建一个IndexAction类代码如下:

java 代码
 
  1. package com.test.struts2;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3. @SuppressWarnings("serial")  
  4. public class IndexAction extends ActionSupport{  
  5.     
  6.     private String userName;这里的个名字要和表单的名字一样  
  7.   
  8.     public String getUserName() {  
  9.         return userName;  
  10.     }  
  11.     public void setUserName(String userName) {  
  12.         this.userName = userName;  
  13.     }  
  14.     public String sayHello() throws Exception {  
  15.         return SUCCESS;  
  16.     }  
  17. }  

建立struts2的核心配置文件:struts.xml代码如下:<o:p></o:p>

xml 代码
 
  1. xml version="1.0" encoding="UTF-8" ?>  
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.     <include file="struts-default.xml" />  
  6.     <package name="default" extends="struts-default">  
  7.         <action name="index" class="com.test.struts2.IndexAction" method="sayHello">  
  8.             <result name="success" type="freemarker">/main1.ftl<!---->result>  
  9.         <!---->action>  
  10.     <!---->package>  
  11. <!---->struts>  
分享到:
评论
6 楼 peifang 2008-12-10  
感谢分享!共同强大中国软件事业!
5 楼 A19850925 2008-05-25  
谢谢了!!!
4 楼 xingkaistart 2008-05-25  
感谢
3 楼 wzltian 2007-05-09  
呵呵,非常感谢,今天终于可以回帖了,谢谢楼主无私的奉献精神
2 楼 javaman1982 2007-04-16  
感谢哈,这个例子我已经学会了,可否再出一些例子哈
1 楼 kiss.988 2007-03-27  
谢谢楼主。作得不错,我支持你。

相关推荐

Global site tag (gtag.js) - Google Analytics