一、为何用Struts2框架
(1)实现MVC模式,结构清晰
(2)丰富的标签(tag)
(3)通过配置文件页面导航,便于后期维护
(4)与Servlet API松耦合,便于测试
二 、到官网下载: http://struts.apache.org
三、目前我使用的是struts-2.3.15
四、开发的具体步骤如下:
1、创建工程,引入Struts 2工程所需运行库文件。
2、创建并配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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></web-app>
Struts2的核心控制器FilterDispatcher被设计成了过滤器,通过<filter></filter>标签引</span>
<url-pattern>/*</url-pattern>说明所有客户端请求都经由FilterDispatcher处理,并把过滤后的请求交给Struts2进行处理。
3、创建一个Action类
package com.hlx.action.login; import com.hlx.dao.LoginDao; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { // 对应表单属性 封装(setXXX and getXXX) private String uname; private String upass; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } @Override public String execute() throws Exception { // System.out.println(10/0); //运行发生的异常 // 调用业务方法 if (LoginDao.isLogin(uname, upass)) { return SUCCESS; } else if ("".equals(uname) && "".equals(upass)) { return ERROR; }else { return INPUT; } } }
Action有一下特点:
4、创建并配置struts.xml文件。
Struts2框架的核心配置文件就是struts.xml,该文件主要负责Struts2应用中各Action的具体实现逻辑。struts.xml会根据不同请求找到具体的Action,然后根据Action返回的逻辑视图名找到具体的物理视图。
<?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="hlx" namespace="/" extends="struts-default"> <!-- 全局结果 --> <global-results> <result name="error">/err.jsp</result> <result name="er_1">/error.jsp</result> </global-results> <!-- 异常映射 --> <global-exception-mappings> <exception-mapping result="er_1" exception="java.lang.RuntimeException"/> </global-exception-mappings> <action name="login1" class="com.hlx.action.login.LoginAction"> <result type="redirect">/index.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
1、登陆页面login.jsp
<h1> 登录 [属性方式]</h1> <form method="post" action="login1" name="frm"> <p> 用户名:<input type="text" name="uname"> </p> <p> 密 码:<input type="password" name="upass"> </p> <p> <input type="submit" value="登录" name="button1"> </p> </form>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ 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 'index.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> <h1>欢迎使用Struts2 </h1><br> <s:property value="uname"/> :<s:property value="upass"/><p/> </body> </html>
<%@ 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 'err.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> 错误页面。。。。。。。。。。。。. <br> </body> </html>