zoukankan      html  css  js  c++  java
  • Maven构建Struts2项目

    1.添加Struts2依赖

    这里主需要在pom.xml中添加一个struts-core的依赖即可:

     1 <project 
     2 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     4   <modelVersion>4.0.0</modelVersion>
     5   <groupId>com.carson.demo</groupId>
     6   <artifactId>struts2</artifactId>
     7   <packaging>war</packaging>
     8   <version>0.0.1-SNAPSHOT</version>
     9   <name>struts2 Maven Webapp</name>
    10   <url>http://maven.apache.org</url>
    11   
    12   <!-- 属性配置 -->  
    13   <properties>  
    14       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    15   </properties>
    16   
    17   <dependencies>
    18   <!-- junit -->
    19     <dependency>
    20       <groupId>junit</groupId>
    21       <artifactId>junit</artifactId>
    22       <version>4.9</version>
    23       <scope>test</scope>
    24     </dependency>
    25     
    26     <!-- struts2依赖包 -->
    27     <dependency>
    28         <groupId>org.apache.struts</groupId>
    29         <artifactId>struts2-core</artifactId>
    30         <version>2.3.14</version>
    31     </dependency>
    32     
    33     
    34     
    35   </dependencies>
    36   <build>
    37     <finalName>struts2</finalName>
    38   </build>
    39 </project>
    View Code

    之后,Maven会自动从网上下载struts2需要的其他依赖包

    2.新建一个Action

    src/main/java目录下新建一个UserAction.java

     1 package com.carson.demo.action;
     2  
     3 import java.io.UnsupportedEncodingException;
     4 import javax.servlet.http.HttpServletRequest;
     5 import javax.servlet.http.HttpServletResponse;
     6  
     7 import org.apache.struts2.ServletActionContext;
     8 import com.opensymphony.xwork2.ActionSupport;
     9  
    10 public class UserAction extends ActionSupport {
    11  
    12     private static final long serialVersionUID = 1L;
    13  
    14     public String execute(){
    15         return SUCCESS;
    16     }
    17     
    18     public String login() {
    19         try {
    20             HttpServletRequest request = ServletActionContext.getRequest();
    21             HttpServletResponse response = ServletActionContext.getResponse();
    22             request.setCharacterEncoding("UTF-8");
    23             response.setContentType("text/html;charset=utf-8");
    24             String username = request.getParameter("username");
    25             String password = request.getParameter("password");
    26             System.out.println("name->" + username + ",password->"
    27                     + password);
    28             if ("admin".equals(username) && "123456".equals(password)) {
    29                 return SUCCESS;
    30             } else {
    31                 return "login";
    32             }
    33         } catch (UnsupportedEncodingException e) {
    34             e.printStackTrace();
    35         }
    36         return SUCCESS;
    37     }
    38 }
    View Code

    3.配置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>
     
        <constant name="struts.i18n.reload" value="false" />
        <constant name="struts.devMode" value="false" />
       
        <include file="struts-default.xml" />
     
        <package name="default" extends="struts-default" namespace="/">
     
            <action name="login" class="com.carson.demo.action.UserAction" method="login">
                <result name="success">index.jsp</result>
                <result name="login">login.jsp</result>
            </action>
     
        </package>
     
    </struts>
    View Code

    4.配置web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
     
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
        <init-param>
            <param-name>config</param-name>
            <param-value>../../resources/struts.xml</param-value>
        </init-param>
        
        <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>login.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    View Code

    5.测试

    新建两个页面login.jsp,index.jsp,内容如下:

     login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"  
        pageEncoding="UTF-8"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
        <head>  
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
            <title>登录界面</title>  
        </head>  
          
        <body> 
    <form action="login" method="post"> 
    <table>
    <tr>
    <td>用户名:</td>
    <td><input type="text" name="username" /> </td>
    </tr>
    <tr>
    <td>密码:</td>
    <td><input type="text" name="password" /> </td>
    </tr>
    <tr>
    <td colspan="2">
    <input type="submit" value="登录" />
    <input type="reset" value="重置" /></td>
    </tr>
    </table>
    </form>
        </body>  
    </html> 
    View Code

    index.jsp页面:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"  
     2     pageEncoding="UTF-8"%>  
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
     4 <html>  
     5     <head>  
     6         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     7         <title>Hello Maven</title>  
     8     </head>  
     9       
    10     <body>  
    11         <p>大家好,欢迎进入Maven Struts2应用!</p>  
    12     </body>  
    13 </html> 
    View Code

    启动之后就可以看到登录页面,至此,用Maven构建Struts2项目完成。

    山重水复疑无路,柳暗花明又一村! 专注填坑,少走弯路!
  • 相关阅读:
    祖传屎山代码
    WebService原理及重要术语
    ML-For-Beginners
    Row Level Security行级数据安全,简称RLS。
    浅析 Dapr 里的云计算设计模式
    讲师征集| .NET Conf China 2021正式启动
    为什么 Dapr 如此令人兴奋
    Open Application Model(OAM)的 Kubernetes 标准实现 Crossplane 项目 成为 CNCF 孵化项目
    kubectl 的插件管理工具krew
    PrimeBlazor 组件以MIT 协议开源
  • 原文地址:https://www.cnblogs.com/mqflive81/p/10645004.html
Copyright © 2011-2022 走看看