Java进阶知识05 Struts2的基础配置详解

1、Struts2的原理/流程步骤  

简单的理解:
    1、客户端发送一个request请求,Tomcat服务器接收到的请求经过web.xml配置文件去处理,进入struts2的核心过滤器,从而进入struts2的struts.xml配置文件。
    2、根据客户端的请求action名称去寻找struts.xml配置文件中对应的action名称,从而去调用action包下对应的action类,经过一系列的逻辑处理后,将处理的结果返回给struts2。
    3、最后struts2将结果返回给客户端并显示出操作后的结果。

2、基础配置(实例解析)  

web.xml 配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12  <!-- struts2过滤器 -->  
13  <filter>
14       <filter-name>struts2</filter-name>
15       <filter-class>
16           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
17       </filter-class>
18   </filter>
19   <filter-mapping>
20       <filter-name>struts2</filter-name>
21       <url-pattern>/*</url-pattern>
22   </filter-mapping>
23     
24 </web-app>

struts2.xml 配置文件(创建在src目录下

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <package name="loginDemo" namespace="/" extends="struts-default" >
 8         <action name="login" class="com.shore.action.LoginAction" >
 9             <result name="success">success.jsp</result>
10             <result name="error">error.jsp</result>
11         </action>
12     </package>
13 </struts>

LoginAction 类

 1 package com.shore.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5 
 6 
 7 /**
 8  * @author DSHORE/2019-9-10
 9  *
10  */
11 public class LoginAction implements Action {
12 
13     private String username;
14     private String password;
15     
16     public void setUsername(String username) {
17         this.username = username;
18     }
19     public void setPassword(String password) {
20         this.password = password;
21     }
22 
23     public String execute() throws Exception {
24         System.out.println("用户名:"+username+" -密码:"+password);
25         if (username.equals("zhangsan") && password.equals("123456")) {
26             return SUCCESS;
27         }else {
28             return ERROR;
29         }
30     }
31 }

index.jsp 页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24     <form action="login.action" method="post" name="loginForm">
25         用户名:<input type="text" name="username" /><br/>
26         密    码:<input type="password" name="password" /><br/>
27         <input type="submit" name="sbm" value="提交" />
28     </form>
29   </body>
30 </html>

运行结果图:

   

 

 

 

 

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11502389.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2019-09-10 19:26  DSHORE  阅读(241)  评论(0编辑  收藏  举报