Struts2 开发步骤 教程(MyEclipse )

目录
[隐藏]

一、开发流程概述

1.在web.xml中定义核心Filter拦截用户请求,添加如下内容:

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

2.根据需求编辑jsp请求代码

3.定义处理用户请求的Action类

4.配置Action,类似如下内容(struts.xml):

        <action name="login" class="cn.no7player.struts2.action.LoginAction">
            <!-- 定义三个逻辑视图和物理资源之间的映射 -->     
            ...    
        </action>

5.配置处理结果和视图资源的关系:

        <action name="login" class="cn.no7player.struts2.action.LoginAction">
            <!-- 定义三个逻辑视图和物理资源之间的映射 -->     
            <result name="input">/login.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>        
        </action>

6.编写视图资源,一般借助OGNL表达式

二、操作实践——创建一个简单的struts2应用

1. 新建 Web Project :

(1)选择创建Web Project项目,命名为:StrutsSimple

ab284677-5de9-48d9-9def-ab36016151dd

(2)创建完成后初始化目录结构如图:

d6738c39-29f2-4880-8e24-b230bc22e416

(3)* index.jsp初始化内容:

<%@ 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 '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>
    This is my JSP page. <br>
  </body>
</html>

(4)*web.xml初始化内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloStruts2</display-name>
  <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.下载配置struts2依赖包:

  (1)  http://struts.apache.org/download.cgi
下载选项:
(2)下载第一个struts-2.3.20-all.zip并解压:
    apps: 示例应用
    docs: 技术文档
    lib: 核心类库
    src: 源代码
(3) * apps–>.war–>struts2-blank.war 解压(一个最简单的示例程序)
      进入lib目录,复制该目录下的所有jar文件到 WebRoot/WEB-INF/lib目录中。

3. 修改配置文件

(1)web.xml—-定义核心Filter拦截用户请求:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2Simple</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)index.jsp—-根据需求编辑jsp请求代码,以及编写视图资源文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title><s:text name="loginPage"/></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>
    <s:form action="login">
            <s:textfield name="username" key="user"/>
            <s:textfield name="password" key="pass"/>
            <s:submit key="login"/>
    </s:form>
  </body>
</html>

welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title><s:text name="succPage"/></title>
    </head>
    <body>
        <s:text name="succTip">
			<s:param>${sessionScope.user}</s:param>
		</s:text><br>
    </body>
</html>

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title><s:text name="errorPage"/></title>
    </head>
    <body>
        <s:text name="failTip"/>
    </body>
</html>

(3)LoginAction.java—-定义处理用户请求的Action类:

package cn.no7player.struts2.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
	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;
	}
	public String execute() throws Exception	{
		if (getUsername().equals("zhoul")&& getPassword().equals("123456") ){
			ActionContext.getContext().getSession().put("user" , getUsername());
			return SUCCESS;
		}
		else{
			return ERROR;
		}
	}
}

(4)struts.xml—-配置Action,配置处理结果和视图资源的关系:

<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default"  namespace="/" extends="struts-default">
        <action name="login" class="cn.no7player.struts2.action.LoginAction">
            <!-- 定义三个逻辑视图和物理资源之间的映射 -->     
            <result name="input">/login.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>        
        </action>
    </package>
    <!-- Add packages here -->
</struts>

(5)目录结构:

64e974da-b76a-41c9-adc7-df534125d5af

分享到:

发表评论

昵称

沙发空缺中,还不快抢~