zoukankan      html  css  js  c++  java
  • Struts

    一、下载Struts

    建立web项目,给项目添加外部引用包(project-properties-Java Build Path-Add External Jars...)。添加的包有:commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,commons-logging-api-1.1.jar,freemarker-2.3.16.jar,javassist-3.7.ga.jar,ognl-3.0.jar,struts2-core-2.2.1.1.jar,xwork-core-2.2.1.1.jar。注意:由于struts2版本的差异性,上面提到的包不一定满足所有版本的需求。配置完struts2后,请部署运行一下。根据运行时的错误提示来添加jar包解决问题。

    二、创建web项目,导入使用struts2所必须的jar包。

    在MyEclipse项目中的src根目录下建立一个struts.xml文件。(可以打开下载的struts2安装包里的apps目录下的任意一个jar包,在里面的WEB_INFR/src目录下,寻找struts.xml文件,将该文件复制进项目的src根目录下,将里面的内容清空(只留下<struts>标签和头部标签即可))。

    四、在web.xml中加入struts2 MVC框架启动配置

      和struts.xml文件的生成类似,在struts2安装包里找到web.xml文件,将里面的<filter>和<filter-mapping>标签及其内容拷贝进项目中的web.config文件即可。

      五、struts2实例--简单的登录例子

         5.1 编写login.jsp页面。代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title>Login</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">

    </head>

      <body>
       <s:form action="/login" method="post">
        <s:label value="系统登陆"></s:label>
        <s:textfield name="username" label="账号" />
        <s:password name="password" label="密码" />
        <s:submit value="登录" />
       </s:form>
      </body>
    </html>

        5.2 编写LoginAction类。代码如下:

    package com.gsww.kingreturns.struts2.excise;

    import com.opensymphony.xwork2.ActionSupport;

    public class LoginAction extends ActionSupport {//该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法

     private static final long serialVersionUID = 1L;
     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;
     }

     @Override
     public String execute() throws Exception {
      if("haha".equals(username) && "hehe".equals(password))//如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN
       return SUCCESS;
      return LOGIN;
     }
    }

        5.3 配置struts.xml文件。代码如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>

        <package name="default" namespace="/" extends="struts-default">
            <action name="login" class="com.gsww.kingreturns.struts2.excise.LoginAction" method="execute">
                <result name="success">/welcome.jsp</result>
                <result name="login">/login.jsp</result>
            </action>
        </package>

    </struts>

    5.4 配置web.xml文件。代码如下:

    <?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">
     
     <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.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    5.5 根据struts.xml里配置的内容,还需要一个welcome.jsp页面。编写welcome.jsp页面。代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title>My JSP 'welcome.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">
     

      </head>
      
      <body>
       欢迎${username }!
      </body>
    </html>

    经过上述步骤,登录实例已经编写完毕。启动tomcat,在网页地址栏里输入:http://localhost:8080/项目的名称/login.jsp,打开登录页面。如下所示:

    输入用户名:haha,密码:hehe,点击登录,就来到了welcome.jsp页面。如下所示:

    如果输入的用户名和密码不是haha和hehe,那么,就来到了login.jsp页面。

  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/lihaiyang123/p/7084600.html
Copyright © 2011-2022 走看看