struts2简单实例项目



struts2开发简单实例

1.引入struts2相应的jar包,可以到网上下载,大把大把的。

2.配置struts.xml 要放在src目录下 如下:
Java代码 收藏代码

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>
<struts>
<!– Struts 2 的Action必须放在包空间下 –>
<package name=”strutsqs” extends=”struts-default”>
<!– 定义action的名字以及action的实现类 –>
<action name=”LoginAciton” class=”com.gjy.struts2.loginAction.LoginAction”>
<!– 定义action的处理结果result,result有两个属性,其中name指定返回名称,tyle指定返回的类型 –>
<!– 处理Login错误时返回/error.jsp –>
<result name=”error”>/error.jsp</result>
<!– 处理Lognin正确时/suc.jsp –>
<result name=”suc”>/suc.jsp</result>
</action>
</package>
</struts>

3.配置web.xml struts2与struts1不同,struts2是通过一系列的过滤器来组成过滤链来对用户的请求作处理
如下:
Java代码 收藏代码

<?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”>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<!–定义struts2的核心Filter的实现类 –>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>

3.写Action,struts2的acction不继承其它类,是一个普通的java类,它不与serverlet API 相偶合,降低了代码的可维护性,使得代码
更加容易测试。
如下:

(1).自定义一个action接口
Java代码 收藏代码
package com.gjy.struts2.interfaceAction;

public interface Action {
public static String SUCCESS=”suc”;
public static String ERROR=”error”;

public String execute()throws Exception;
}

(2).写一个loginAction,实现action接口,此action的业务逻辑没有与LoginAction分开,可自行实现分开
Java代码 收藏代码
package com.gjy.struts2.loginAction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.gjy.struts2.entity.Book;
import com.gjy.struts2.interfaceAction.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action {

private String userName;
private String passWord;

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;
}

//处理用护请求的execute方法
public String execute() throws Exception
{

if (getUserName().equals(“gongjiayun”)
&& getPassWord().equals(“hehe”) )
{
//通过ActionContext访问Web对象的Session对象,此处的ActionContext返回的是一个Map
//虽然struts2的action中没有HttpSession对象,但我们也可以通过通过ActionContext访问Web对象的Session对象
Map sessionMap = ActionContext.getContext().getSession();
sessionMap.put(“user” , getUserName());
Book book = new Book();
Object books[]= book.getBook();
List<Object[]> bookList = new ArrayList<Object[]>();
bookList.add(books);
sessionMap.put(“book”, bookList);
return SUCCESS;
}
else
{
return ERROR;
}
}
}
(3).写一个实体类Book
Java代码 收藏代码
package com.gjy.struts2.entity;

public class Book {
private static Object book[]=new Object[4];

public Book(){
book[0] = “《java编程思想》”;
book[1] = “《Struts2权威指南》”;
book[2] = “《精通Oracle》”;
book[3] = “《我的Flex我精通》”;
}


public Object[] getBook(){
return book;
}
}

(4).写一个登陆页面login.jsp
Java代码 收藏代码
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<base href=”<%=basePath%>”>

<title>My JSP ‘login.jsp’ starting page</title>

<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”>
<!–
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
–>

</head>

<body>
<p>用户登陆</p>
<form action=”LoginAciton.action”>
username:<input type=”text” id=”userName” name=”userName”/><br/><br/>
password:<input type=”password” id=”passWord” name=”PassWord”/><br/><br/>
<input type=”submit” value=”submit”>
</form>
</body>
</html>

(5).写一个显示页面suc.jsp
Java代码 收藏代码
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=”s” uri=”/struts-tags”%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<base href=”<%=basePath%>”>

<title>My JSP ‘suc.jsp’ starting page</title>

<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”>
<!–
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
–>
</head>

<body>

<center><div>
WelCome,${sessionScope.user},you can do anything! Long Long ago,I have a dream !<br>
<table border=”1px” bordercolor=”blue” id=”tbColor”>
<caption>作者gjy的图书</caption>
<c:forEach var=”books” items=”${book}” >
<tr >
<td>书名:</td>
<td>${books[0] }</td>
</tr>
<tr>
<td>书名:</td>
<td>${books[1] }</td>
</tr>
<tr>
<td>书名:</td>
<td>${books[2] }</td>
</tr>
<tr>
<td>书名:</td>
<td>${books[3] }</td>
</tr>
</c:forEach>
</table>
</div></center>
</body>
</html>

(6).写一个error.jsp
Java代码 收藏代码
<%@ page language=”java” import=”java.util.*” pageEncoding=”ISO-8859-1″%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<base href=”<%=basePath%>”>

<title>My JSP ‘suc.jsp’ starting page</title>

<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”>
<!–
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
–>

</head>

<body>
error,you can do nothing! <br>
</body>
</html>

http://gongjiayun.iteye.com/blog/802000