Strus2学习:基础(一)

Strus2基础:

Sturs2起源以及背景:

  在起源很早(2002年左右)的 strus1 和 webWork 基础上进行扩展,并且兼容这两大框架!总之很好用啦,随着学习的深入,应该会有更好的诠释的!


 新建strus2项目:

  基础架包:

  strus2-core-***.jar    核心类库

  xwork-core-***.jar   xwork类库。构建基础

  ognl-**.jar       表达式语言类库

  freemarker-**.jar    标签模板类库

  javassist-**.GA.jar   字节码处理

  commons-fileupload-**.jar  文件上传

  commons-io-**.jar      io-扩展

  Commons-lang-**.jar  数据类型的工具类

  

创建第一个strus2网页:

  以一个简单的登录操作来了解strus2执行过程

  默认配置名:struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="test" class="demo.firstAction.FirstAction">
            <result name="success">index.jsp</result>
        </action>
        <action name="login" class="demo.firstAction.FirstAction">
            <result name="success">index.jsp</result>
            <result name="erro">erro.jsp</result>
        </action>
    </package>
</struts>
struts.xml

  action实现类:

package demo.firstAction;



import com.opensymphony.xwork2.Action;

public class FirstAction implements Action{
    
    //用户输入姓名
    private String name="";
    //向用户显示的信息
    private String message="";
    @Override
    public String execute() throws Exception {
        if("李四".equals(name)){
            this.setMessage("hello,"+this.getName()+"!");
            return "success";
        }else{
            this.setMessage("用户名:"+this.getName()+"登录失败!");
            return "erro";
        }
        
    }
    
    
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

}
FirstAction.java

  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">
 <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>
web.xml

  页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 导入strus2标签库 -->
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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>
  <s:property value="message"/>
  <form action="login.action" method="post">
  
  <input type="text" name="name"/>
  <input type="submit" value="登录">
  </form>
    
  </body>
</html>


<!--登录失败-->

<body>
   <h1>登录失败界面</h1>
   <s:property value="message"/>
  </body>
View Code

执行流程:

  

提醒:

  excute方法是Action接口的重写方法 ,struts会默认执行这个方法

了解了他的执行过程,当出现exception时就好解决多 了!

下面是我今天遇到的两个问题:

Strus2初步学习遇到的exception

错误一:

 

分析:

using Struts tags without the associated filter

没有关联过滤器

我只在页面上写了struts标签却没去配置struts.xml&书写相应的java代码

解决办法:

把代码写完整

错误二:启动tomcat时报错,提示class没找到,这个类不是java类库里面的从org/apeche/commons/lang3可以看出,所以导入缺少的架包

 

添加缺少的架包:问题解决

 

原文地址:https://www.cnblogs.com/gcs1995/p/4208186.html