struts2包可以在struts2官方网址下载,struts2采用热部署的方式注册插件,即如果向struts2中添加插件,直接把jar文件放在lib中即可,而不需要配置任何文件。
新建Web项目struts2,将struts2所以来的jar添加到/WEB-INF/lib文件夹,注意删除没有用的插件(形如xxx-plugin-2.0.11.jar的)。先用struts2实现登录功能,新建Action类LoginAction。struts2的Action继承com.opensymphony.xwork2.ActionSupport类。源代码如下:
LoginAction
import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String account; //账号 private String password; //密码 @Override public String execute() throws Exception { if("helloween".equalsIgnoreCase(account) && "1234".equals(password)){ return SUCCESS; } return LOGIN; } public String getAccount() { //account属性的getter方法 return account; } public void setAccount(String account) { //account属性的setter方法 this.account = account; } public String getPassword() { //password属性的getter方法 return password; } public void setPassword(String password) { //password的setter方法 this.password = password; } }
LoginAction中有两个属性account password,代表JSP表单的两个输入框,Struts2会自动把输入框内容getter和setter进来。另外还有一个execute()方法,这是Struts2的住方法提交数据后Struts2会调用该方法,返回值代表结构页面的名称,具体路径在配置文件中
Struts2配置文件
Struts2文件默认位置为/WEB-INF/classes/struts.xml,可以将Struts.xml放到Myeclipse文件夹下,代码如下:
<?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> <package name="main" extends="struts-default"> <global-results> <!-- 所有的全局变量result --> <result name="login">/login.jsp</result> </global-results> <action name="loginPerson" class="com.fcl.action.LoginAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
JSP登录界面
jsp中使用struts2显示数据。注意添加struts2标签的taglib声明。struts2标签的用法与struts1的完全不一样。代码如下:
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="struts"%> <!-- 标签库taglib声明 --> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录成功</title> </head> <body> 登录成功,欢迎您,<struts:property value="account"/> <!-- 显示Action里的account属性 --> </body> </html>
全局登录页面login.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="struts"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录</title> </head> <body> <struts:form action="loginPerson"> <!-- FORM表单 --> <struts:label value="登录系统"></struts:label> <struts:textfield name="account" label="账号"></struts:textfield> <struts:password name="password" label="密码"></struts:password> <struts:submit value="登录"></struts:submit> </struts:form> </body> </html>
两个页面都用了struts2标签
配置web.xml
最后,在web.xml中配置分配器,struts2使用filter作为分发器。如果有多个filter,要把struts2的分发器filter作为最后一个filter。代码如下:
<?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> <!-- Struts的filter,所有的请求都被映射到Struts2上 --> <filter> <filter-name>struts2</filter-name> <!-- filter的名称 --> <filter-class> <!-- filter的入口 --> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <!-- 该参数可省略,默认为"*.action" --> <param-name>struts2.action.extension</param-name> <param-value>action</param-value> <!-- 默认为"*.action --> </init-param> </filter> <!-- Strut2的Filter的URL --> <filter-mapping> <filter-name>struts2</filter-name> <!-- Filter名称 --> <url-pattern>/*</url-pattern> <!-- 截获所有URL --> </filter-mapping> </web-app>
struts2默认的后缀为“.action".配置<url-pattern>最好配置为“/*”而不要只配置为“*.action"。应为struts2集成了一些js资源,而这些资源并不是都是以.action结尾的.