---
title: "Struts2入门实例"
author: "esffor"
date: "2008-02-19T09:07:52Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2008-02-19T09:07:52Z"
id: 168185
url: "https://esffor.iteye.com/blog/168185"
---

web.xml

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;web-app version="2.5"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xmlns="http://java.sun.com/xml/ns/javaee"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/web-app&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 srtuts.xml (WEB-INF/classes下)

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="GBK" ?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE struts PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;struts&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;package name="struts" extends="struts-default"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;action name="Login" class="HelloWorld.LoginAction"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;result name="success"&gt;/helloworld/welcome.jsp&lt;/result&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;result name="error"&gt;/helloworld/error.jsp&lt;/result&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;/action&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/package&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/struts&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

Action：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package HelloWorld;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.Action;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.ActionContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class LoginAction implements Action...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  private String username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  private String password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  private String\[] books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  public String\[] getBooks() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setBooks(String\[] books) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.books = books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String execute() throws Exception...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      if(this.getUsername().equals("admin")&&this.getPassword().equals("1234"))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          ActionContext.getContext().getSession().put("user", this.getUsername());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          BookService bs=new BookService();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this.setBooks(bs.getBooks());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return SUCCESS;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      }else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return ERROR;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)      }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getUsername() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setUsername(String username) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.username = username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getPassword() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setPassword(String password) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.password = password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

BookService.java

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package HelloWorld;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class BookService ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  private String\[] books=new String\[]...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          "Spring 2.0",  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          "Hibernate 3.2",  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          "Struts 2.0"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  };  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  public String\[] getBooks()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      return books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

index.jsp

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page language="java" contentType="text/html; charset=gb2312"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    pageEncoding="gb2312"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ taglib prefix="s" uri="/struts-tags" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;head&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;title&gt;&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;body&gt;        
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;s:form action="Login"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:textfield name="username" label="用户名"&gt;&lt;/s:textfield&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:password name="password" label="密码"&gt;&lt;/s:password&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:submit value="login"&gt;&lt;/s:submit&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;/s:form&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

welcome.jsp

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page language="java" contentType="text/html; charset=gb2312"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    pageEncoding="gb2312"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page isELIgnored="false" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ taglib prefix="s" uri="/struts-tags" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;head&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;title&gt;&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;body&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   欢迎${sessionScope.user}登陆  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;c:out value="${requestScope.now}"&gt;&lt;/c:out&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;br&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;table border="1"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;s:iterator value="books" status="index"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;s:if test="#index.odd==true"&gt;