• 【struts2】学习笔记


    • 常见问题及注意事项: 

      1.下载struts2时,要看清所下载的版本,不同版本web.xml配置路径不同!

       2. 导入jar包时,导入的包要完全准确,缺少或过多的会导致缺失或冲突!

       3. RegisterAction-validation.xml 客户端校验无效,暂未解决。

     

    1. 下载struts2软件包
      
    下载地址:http://struts.apache.org/download.cgi#struts2513
    2. 在web工程中导入struts包中的jar文件
      
    2.1 将下载好的struts包下的一些核心类库复制到/WEB-INF/lib目录下,并构建路径。

                   

     

    3.  web.xml配置

      3.1 针对不同struts版本,web.xml配置方式有所不同!

        check your struts2-core-x.x.jar version.

          -->if it is struts2-core-2.5.jar then change your filter class tag value in web.xml to

          <filter-class>
               org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
           </filter-class>
    

          -->if it is struts2-core-2.1.3.jar then change your filter class tag value in web.xml to

          <filter-class>
               org.apache.struts2.dispatcher.FilterDispatcher
           </filter-class>
    

        FilterDispatcher is deprecated since Struts 2.1.3. If you are working with older versions then user above solution.

          -->if it is struts2-core-2.3.X.jar then change your filter class tag value in web.xml to

          <filter-class> 
              org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
          </filter-class>

      3.2 配置web.xml(本人struts版本为struts2.3)

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

    4. struts配置实例struts.xml
      <?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>
        <package name="struts2" extends="struts-default">
            <action name="login" class="com.gxh.action.LoginAction">
                <result name="success">/login_success.jsp</result>
                <result name="failure">/login_failure.jsp</result>
                <result name="input">/login.jsp</result>
            </action>
            <action name="input" class="com.gxh.action.InputAction">
                <result name="success">/demo/output.jsp</result>
                <result name="input">/demo/input.jsp</result>
            </action>
            <action name="register" class="com.gxh.action.RegisterAction">
                <result name="success">/demo2/result.jsp</result>
                <result name="input">/demo2/register2.jsp</result>
            </action>     
        </package>
      </struts> 

    5. 创建控制类Action
       
    //LoginCheck.java类,校验用户名和密码
        public class LoginCheck{
          public boolean isLogin(String username,String password){
            if("tom".equals(username) && "123".equals(password)){
              return true;
            }else{
              return false;
            }
          }
        }

      方式一:编写普通的实体类,如loginAction.java ,重写execute()方法
        public class LoginAction{
          private String username;
          private String password;

          public void setUsername(String username){
            this.username = username;
          }
          public String getUsername(){
            return username;
          }
          public void setPassword(String password){
            this.password= password;
          }
          public String getPassword(){
            return password;
          }

          
          public String execute()throws Exception{
            LoginCheck lc = new LoginCheck();    //LoginCheck.java类另写
            if(lc.isLogin(getUsername(),getPassword())){
              ActionContext.getContext().getSession().put("login","true");
              return "success";
            }else{
              return "failure";
            }
          }
        }
        //配置struts.xml
         <?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>
          <package name="struts2" extends="struts-default">
            <action name="login" class="com.gxh.action.LoginAction">
                <result name="success">/login_success.jsp</result>
                <result name="failure">/login_failure.jsp</result>
            </action>             
          </package>
        </struts> 

       
      方式二:编写Java类,实现Action接口,如LoginAction2.java
        public class LoginAction2 implements Action{
          public String username;
          public String password;

          public void setUsername(String username){
            this.username = username;
          }
          public String getUsername(){
            return username;
          }
          public void setPassword(String password){
            this.password = password;
          }
          public String getPassword(){
            return password;
          }

          public String execute()throws Exception{
            LoginCheck lc = new LoginCheck();    //LoginCheck.java类另写
            if(lc.isLogin(getUsername(),getPassword())){
              ActionContext.getContext().getSession().put("login","true");
              return SUCCESS;
            }else{
              return ERROR;
            }
          }
       

        }
        //配置struts.xml
         <?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>
          <package name="struts2" extends="struts-default">
            <action name="login" class="com.gxh.action.LoginAction2">
                <result name="success">/login_success.jsp</result>
                <result name="error">/login_failure.jsp</result>
            </action>             
          </package>
        </struts> 

      方式三:编写Java类,继承ActionSupport类,如LoginAction3.java
        public class LoginAction3 extends ActionSupport{
          public String username;
          public String password;

          public void setUsername(String username){
            this.username = username;
          }
          public String getUsername(){
            return username;
          }
          public void setPassword(String password){
            this.password = password;
          }
          public String getPassword(){
            return password;
          }
          
          pubic void validate(){
            if(getUsername() == null || "".equals(getUsername().trim()) ){
              this.addFieldError("username","username can not empty.");  //username为在username标签上提示错误,
            }else if(!getUsername().matches("正则式")){    //
              this.addFieldError("username","username is invalidate.");
            }
            if(getPassword() == null || "".equals(getPassword().trim())){
              this.addFieldError("password","password can not empty.");  //password为在password标签上提示错误,
            }else if(!getPassword().matches("正则式")){
              this.addFieldError("password","password is invalidate.");
            }
          }
          public String execute()throws Exception{
            LoginCheck lc = new LoginCheck();    //LoginCheck.java类另写
            if(lc.isLogin(getUsername(),getPassword())){
              ActionContext.getContext().getSession().put("login","true");
              return SUCCESS;
            }else{
              return ERROR;
            }
          }
       
        }

        //配置struts.xml
         <?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>
          <package name="struts2" extends="struts-default">
            <action name="login" class="com.gxh.action.LoginAction3">
                <result name="success">/login_success.jsp</result>
                <result name="error">/login_failure.jsp</result>
               <result name="input">/login.jsp</result>  //当验证不通过时,跳转到登录页面
            </action>             
          </package>
        </struts> 

    • struts学习总结

     1. struts应用

    • 单设计,使用struts标签替换传统的HTML标签;
    • 使用Action-validation.xml实现客户端校验;
    • 通过重写validate()方法实现服务器端校验。

      

  • 相关阅读:
    C语言文法
    实验一
    词法分析
    py中文词频统计
    py字符串练习
    py画了个国旗
    熟悉常用的Linux操作
    大数据概述
    实验三、 递归下降分析程序实验
    简易c语言LL(1)文法
  • 原文地址:https://www.cnblogs.com/guoxh/p/7593882.html
Copyright © 2020-2023  润新知