一切福田,不離方寸,從心而覓,感無不通。

Struts2开发环境搭建,及一个简单登录功能实例

首先是搭建Struts2环境。

第一步 下载Struts2
去Struts官网 http://struts.apache.org/ 下载Struts2组件。
截至目前,struts2最新版本为2.3.1.3,下载struts-2.3.16.3-all.zip,解压,放着。

第二步 新建Web Project并导入jar包
在MyEclispe中新建Web Project,然后找到解压的Struts2包,在里面apps文件夹下找到struts2-blank.war,解压这个WAR文件,将里面WEB-INF\lib目录下的jar文件全部复制到新建的Web Project的WebRoot\WEB-INF\lib目录下。

第三步 配置web.xml
在项目的WebRoot\WEB-INF\目录下找到web.xml文件,没有就新建一个web.xml文件,在里面添加如下代码:

[2.3]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>web1</display-name>
<filter>
<filter-name>struts2</filter-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

[2.5]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>web1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

第四步 配置struts.xml
在项目的src目录下找到struts.xml文件,没有就新建一个,里面代码如下:

[2.3]

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

<struts>
<package name="main" extends="struts-default">
</package>
</struts>

[2.5]

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

<struts>
<package name="main" extends="struts-default">
</package>
</struts>

到此,Struts2开发环境搭建完成。

 

下面演示一个登录页面实例。

第一步 修改Index.jsp
修改Index.jsp,做出登录界面。
下面是index.jsp的代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>登录</title>
</head>

<body>
<form action="login" method="post">
登录<br />
账号:<input type="text" name="username" /><br />
密码:<input type="password" name="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>

第二步 编写验证账户和密码的类
新建LogAction类,让其继承com.opensymphony.xwork2.ActionSupport类,注意到index.jsp中两个输入框的name属性分别是username和password,所以LogAction类必须包含下面两个属性:
private String username
private String password
而且必须写出他们的get、set方法。

然后,编写execute方法,在execute方法中验证账号和密码并返回String类型的结果,execute方法会在该Action类被调用的时候自动执行。

下面是LogAction.java的完整代码:

package com.lidi.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class LogAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private String username;//账号
private String password;//密码

//getters & setters
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方法会在该Action类被调用的时候自动执行,
* 如果 账号="admin"并且密码="123456",就返回SUCCESS
* 否则返回ERROR
*/
public String execute(){
if(username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("123456")){
return SUCCESS;
}
else
return ERROR;
}
}

上面的返回SUCCESS和返回ERROR什么意思呢,后面再讲。

第三步 配置struts.xml
第二步编写了Action类,第三步将该Action配置到struts.xml中,打开struts.xml,在package标签中添加如下代码:
<action name="login" class="com.lidi.struts.action.LogAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>

action标签的name属性为login,这个必须与index.jsp中form标签的action属性一致,class属性填写LogAction类的全称。

<result name="success">success.jsp</result> 这个标签的意思是当LogAction类的execute方法返回SUCCESS时,页面跳转到success.jsp;同理,<result name="success">success.jsp</result> 这个标签的意思是当LogAction类的execute方法返回ERROR时,页面跳转到error.jsp。
这里用到了success.jsp和error.jsp,在项目中新建这两个文件,success.jsp表示登录成功后的页面,上面显示登录用的账户和密码,error.jsp表示登录失败后的页面,上面显示错误提示就就好了,他们的代码分别如下:

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML>
<html>
<head>
<title>登陆成功</title>
</head>

<body>
欢迎<s:property value="username" />,登录成功!<br />
</body>
</html>

<%@ taglib prefix="s" uri="/struts-tags"%>表示引用struts标签库
<s:property value="username" />是struts标签,用以显示登录页面传递过来的账号。

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>登录失败</title>
</head>

<body>
登录失败!用户名或密码错误!
</body>
</html>

第四步 运行
配置struts.xml后要重启下服务器,然后在浏览器中查看效果。
分别输入账号和密码并登录,如果账号和密码分别为admin和123456,页面就会显示
欢迎admin,登录成功!
否则就会显示
登录失败!用户名或密码错误!

第五步 程序运行原理浅析
用户填写账号密码并点击登录后,浏览器会请求form标签action属性里面的链接,也就是login,
服务器中,过滤器拦截到login这个请求,就会查找struts.xml中name=login的action,再找到这个action的class属性对应的类,也就是com.lidi.struts.action.LogAction,然后实例化一个LogAction对象,并且把参数username和password分别赋给这个对象的username和passwrod属性(这就是为什么LogAction类的两个属性名称必须和index.jsp中两个文本框的name属性分别相同,并且必须添加他们的get和set方法),然后执行这个对象的execute方法,并返回一个字符串,如果返回SUCCESS字符串,就查找struts.xml中对应action的<result>标签中name属性等于success的<result>标签,并将页面转到该标签里面配置的页面success.jsp上。

from:http://www.cnblogs.com/dige1993/p/4603473.html