`
esffor
  • 浏览: 1362909 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2入门实例

阅读更多

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns
="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
<filter>
      
<filter-name>struts2</filter-name>
      
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>
    
<filter-mapping>
      
<filter-name>struts2</filter-name>
      
<url-pattern>/*</url-pattern>
    
</filter-mapping>


</web-app>

 srtuts.xml (WEB-INF/classes下)

 

<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>
    
<package name="struts" extends="struts-default">
       
<action name="Login" class="HelloWorld.LoginAction">
         
<result name="success">/helloworld/welcome.jsp</result>
         
<result name="error">/helloworld/error.jsp</result>
       
</action>
    
</package>
</struts>

 

Action:

 

package HelloWorld;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action...{
  
private String username;
  
private String password;
  
private String[] books;
  
public String[] getBooks() ...{
    
return books;
}

public void setBooks(String[] books) ...{
    
this.books = books;
}

public String execute() throws Exception...{
      
if(this.getUsername().equals("admin")&&this.getPassword().equals("1234"))...{
          ActionContext.getContext().getSession().put(
"user"this.getUsername());
          BookService bs
=new BookService();
          
this.setBooks(bs.getBooks());
          
return SUCCESS;
      }
else...{
          
return ERROR;
      }

  }

public String getUsername() ...{
    
return username;
}

public void setUsername(String username) ...{
    
this.username = username;
}

public String getPassword() ...{
    
return password;
}

public void setPassword(String password) ...{
    
this.password = password;
}

  
}

 

BookService.java

 

package HelloWorld;

public class BookService ...{
  
private String[] books=new String[]...{
          
"Spring 2.0",
          
"Hibernate 3.2",
          
"Struts 2.0"
  }
;
  
public String[] getBooks()...{
      
return books;
  }

}

 

index.jsp

 

<%...@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding
="gb2312"
%>
<%...@ taglib prefix="s" uri="/struts-tags" %>
<html>
  
<head> 
    
<title></title>
  
</head>
  
<body>      
     
<s:form action="Login">
       
<s:textfield name="username" label="用户名"></s:textfield>
       
<s:password name="password" label="密码"></s:password>
       
<s:submit value="login"></s:submit>
     
</s:form>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
   
  
</body>
</html>

 

welcome.jsp

 

<%...@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding
="gb2312"
%>
<%...@ page isELIgnored="false" %>
    
<%...@ taglib prefix="s" uri="/struts-tags" %>
<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  
<head> 
    
<title></title>
  
</head>
  
<body>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
   欢迎${sessionScope.user}登陆
   
<c:out value="${requestScope.now}"></c:out>
   
<br>
   
<table border="1">
     
<s:iterator value="books" status="index">
         
<s:if test="#index.odd==true">
分享到:
评论
1 楼 yinghuayu1324117 2011-04-18  
不错,要是有对每个模块的注释就更好了

相关推荐

    struts2入门实例

    Struts2入门实例通常会涉及如何创建一个基本的web应用,实现对数据的增删查改操作,并与数据库进行交互。 在"struts2入门实例"中,我们首先会接触到Struts2的核心概念。这包括Action类,它扮演控制器的角色,负责...

    struts2入门实例教程详解

    ### Struts2入门实例教程详解 #### 一、环境搭建与基本配置 在开始学习Struts2框架之前,首先需要确保开发环境的正确搭建。根据提供的文档,所使用的开发工具为MyEclipse6,Web服务器为Tomcat6,Struts版本为...

    Struts2 入门实例代码

    这个入门实例代码将帮助初学者理解Struts2的核心概念和基本用法。 首先,让我们详细了解Struts2框架的关键组成部分: 1. **Action类**:这是业务逻辑的入口点,处理用户请求并执行相应的操作。在Struts2中,一个...

    struts2入门实例和工作(流程)原理

    本文将深入探讨Struts2的入门实例、工作原理及其主要组件。 首先,让我们从一个简单的Struts2入门实例开始。创建一个基本的Struts2应用通常包括以下几个步骤: 1. 引入Struts2的依赖库到项目中,这通常通过Maven或...

    struts2入门实例代码,sourceforge.net经典

    这个"struts2-tutorial-lesson2.zip"压缩包包含了一个基础的Struts2入门实例,是SourceForge.net上的经典教学资源。SourceForge作为一个开源项目托管平台,为开发者提供了许多高质量的学习材料。 这个入门实例...

    struts2入门实例2 经典入门必备

    1.Struts2_01_login 对应登录。。 login.jsp---------------------------------------html标签的登陆页面 login_struts2.jsp-------------------------------采用struts标签的登陆页面 login_struts_...

    struts2入门实例1

    struts2 最新的入门实例 我自己总结的 呵呵,欢迎提出宝贵的意见 1.Struts2_01_login 对应登录。。 login.jsp---------------------------------------html标签的登陆页面 login_struts2.jsp--------------...

    struts2入门实例3 经典入门必备

    1.Struts2_01_login 对应登录。。 login.jsp---------------------------------------html标签的登陆页面 login_struts2.jsp-------------------------------采用struts标签的登陆页面 login_struts_...

    struts2入门实例4 经典入门必备

    1.Struts2_01_login 对应登录。。 login.jsp---------------------------------------html标签的登陆页面 login_struts2.jsp-------------------------------采用struts标签的登陆页面 login_struts_...

    struts2入门例子

    这个"struts2入门例子"旨在帮助初学者理解Struts2的基本架构和核心功能,通过实际操作来学习如何配置Struts2框架以及实现页面跳转。 在Struts2中,`struts.xml`是核心配置文件,它是整个应用的入口点,负责定义动作...

    struts2入门小实例

    本入门实例将带你逐步了解Struts2的基础知识,帮助你快速掌握其核心概念。 首先,我们需要理解Struts2框架的基本架构。它基于拦截器(Interceptor)机制,通过配置Action类和结果视图来处理用户请求。Action类是...

Global site tag (gtag.js) - Google Analytics