• SSH构造struts2项目


    第一在pom.xml导入相应的包

    (网上有很多导入多个包的教程,我缩减到一个了)

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     3     <modelVersion>4.0.0</modelVersion>
     4     <groupId>com.lgp</groupId>
     5     <artifactId>maven_struts2</artifactId>
     6     <packaging>war</packaging>
     7     <version>0.0.1-SNAPSHOT</version>
     8     <name>maven_struts2 Maven Webapp</name>
     9     <url>http://maven.apache.org</url>
    10     <properties>
    11         <project.build.sourceEncoding>
    12             UTF-8
    13         </project.build.sourceEncoding>
    14     </properties>
    15 
    16     <dependencies>
    17         <dependency>
    18             <groupId>junit</groupId>
    19             <artifactId>junit</artifactId>
    20             <version>3.8.1</version>
    21             <scope>test</scope>
    22         </dependency>
    23         <dependency>
    24             <groupId>org.apache.struts</groupId>
    25             <artifactId>struts2-core</artifactId>
    26             <version>2.3.31</version>
    27         </dependency>
    28     </dependencies>
    29     <build>
    30         <finalName>maven_struts2</finalName>
    31     </build>
    32 </project>

    导入包之后,有的电脑会报错,maven-update就好了

    然后去web.xml那里注册struts2的过滤器

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app>
     6     <display-name>Archetype Created Web Application</display-name>
     7     <filter>
     8         <filter-name>struts2</filter-name>
     9         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    10     </filter>
    11 
    12     <filter-mapping>
    13         <filter-name>struts2</filter-name>
    14         <url-pattern>/*</url-pattern>
    15     </filter-mapping>
    16 </web-app>

    然后就写struts.xml,记住放在resources文件夹下

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     4     "http://struts.apache.org/dtds/struts-2.0.dtd">
     5 
     6 <struts>
     7     <!-- name:包名,可自定义 extends:继承 namespace:命名空间 -->
     8     <!-- 定义常量 请求后缀 默认是.action -->
     9     <!-- 但指定了之后 就必须写上后缀 -->
    10     <constant name="struts.action.extension" value="action,do" />
    11     <package name="helloworld" extends="struts-default" namespace="/">
    12         <action name="helloworld_*" class="com.action.HelloWorldAction"
    13             method="{1}">
    14             <result name="{1}">{1}.jsp</result>
    15         </action>
    16     </package>
    17     <package name="user" namespace="/" extends="struts-default">
    18         <interceptors>
    19             <interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"></interceptor>
    20             <interceptor-stack name="mystack">
    21                 <interceptor-ref name="defaultStack" />
    22                 <interceptor-ref name="myInterceptor" />
    23                 <!-- 先进入默认拦截器,再到自定义拦截器 -->
    24             </interceptor-stack>
    25         </interceptors>
    26         <action name="user_login" class="com.action.UserAction" method="login">
    27             <interceptor-ref name="mystack"></interceptor-ref>
    28             <result name="success">userinfo.jsp</result>
    29         </action>
    30     </package>
    31 </struts>

    action类

     1 package com.action;
     2 
     3 public class HelloWorldAction {
     4     public String a() {
     5         System.out.println("a..");
     6         return "a";
     7     }
     8 
     9     public String b() {
    10         System.out.println("b..");
    11         return "b";
    12     }
    13 
    14     public String c() {
    15         System.out.println("c..");
    16         return "c";
    17     }
    18 
    19     public String user() {
    20         System.out.println("user..");
    21         return "user";
    22     }
    23 }
    24 
    25 
    26 
    27 
    28 package com.action;
    29 
    30 import com.entiy.User;
    31 import com.opensymphony.xwork2.ActionContext;
    32 import com.opensymphony.xwork2.ActionSupport;
    33 import com.opensymphony.xwork2.util.ValueStack;
    34 
    35 public class UserAction extends ActionSupport {
    36     private static final long serialVersionUID = -1417237614181805435L;
    37     private String name;
    38     private String pwd;
    39 
    40     public String login() {
    41         System.out.println("login..");
    42         System.out.println("name----" + name);
    43         System.out.println("pwd----" + pwd);
    44         // ValueStack vs=ActionContext.getContext().getValueStack();
    45         ActionContext context = ActionContext.getContext();
    46         ValueStack vs = context.getValueStack();
    47         // 值栈的栈顶
    48 
    49         User user = new User("张三", "张三的密码");
    50         vs.push(user);
    51         return SUCCESS;
    52     }
    53 
    54     public String getName() {
    55         return name;
    56     }
    57 
    58     public void setName(String name) {
    59         this.name = name;
    60     }
    61 
    62     public String getPwd() {
    63         return pwd;
    64     }
    65 
    66     public void setPwd(String pwd) {
    67         this.pwd = pwd;
    68     }
    69 }

    实体类

     1 package com.entiy;
     2 
     3 public class User {
     4     private String name;
     5     private String pwd;
     6 
     7     public User(String name, String pwd) {
     8         this.name = name;
     9         this.pwd = pwd;
    10     }
    11 
    12     public String getPwd() {
    13         return pwd;
    14     }
    15 
    16     public void setPwd(String pwd) {
    17         this.pwd = pwd;
    18     }
    19 
    20     public String getName() {
    21         return name;
    22     }
    23 
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27 }

    拦截器

     1 package com.interceptor;
     2 
     3 import com.opensymphony.xwork2.ActionInvocation;
     4 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
     5 
     6 /**
     7  * 自定义拦截器
     8  * 
     9  * @author SUMMER
    10  *
    11  */
    12 public class MyInterceptor extends AbstractInterceptor {
    13 
    14     /**
    15      * 
    16      */
    17     private static final long serialVersionUID = 1L;
    18 
    19     /**
    20      * 判断session有关的操作
    21      */
    22     @Override
    23     public String intercept(ActionInvocation actioninvocation) throws Exception {
    24         System.out.println("我的拦截器开始...");
    25         actioninvocation.invoke();
    26         System.out.println("我的拦截器结束...");
    27         // AOP 面向切面编程
    28         return null;
    29     }
    30 
    31 }

    a.jsp/b.jsp/c.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>Insert title here</title>
    </head>
    <body>
    aaaaaaaaaaaaaaaa
    </body>
    </html>

    index.jsp

    a没有后缀所以是错误的

     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>Insert title here</title>
     8 </head>
     9 <body>
    10    <a href="<%=request.getContextPath() %>/helloworld_a">a</a>
    11    <br><br>
    12    <a href="<%=request.getContextPath() %>/helloworld_b.do">b</a>
    13    <br><br>
    14    <a href="<%=request.getContextPath() %>/helloworld_c.action">c</a>
    15    <br><br>
    16    <a href="<%=request.getContextPath() %>/helloworld_user.action">用户</a>
    17 </body>
    18 </html>

    user.jsp/userinfo.jsp

    姓名分成3组

    第二第三组是一对的,第一是混搭的,注意取值的方法

     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>Insert title here</title>
     8 </head>
     9 <body>
    10 <form action="<%=request.getContextPath() %>/user_login.do" method="post">
    11     姓名:<input type="text" name="name">
    12     <br><br>
    13     密码:<input type="text" name="pwd">
    14     <br><br>
    15     <input type="submit" value="登录">
    16 </form>
    17 </body>
    18 </html>
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8" isELIgnored="false"%>
     3 <%@taglib uri="/struts-tags" prefix="s"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11     姓名1:${name}
    12     <br>密码1:<%=request.getParameter("pwd")%><br>
    13     <br>姓名2:
    14     <s:property value="[0].name" />
    15     <br>密码2:${requestScope.pwd}
    16     <br>姓名3:
    17     <s:property value="[1].name" />
    18     <br>密码3:
    19     <s:property value="[1].pwd" />
    20     <br>
    21 </body>
    22 </html>
  • 相关阅读:
    eclipse中解决git分支合并冲突
    git 放弃本地修改,强制拉取更新
    廖雪峰Git入门教程
    如何实现Proxifier只代理部分程序
    Proxifier代理工具简介和下载
    Navicat Premium 12.0.18安装与激活
    Teamviewer远程控制
    夜神安卓模拟器下载及简介
    Fiddler2如何对Android应用进行抓包
    谷歌浏览器添加JSON-handle插件
  • 原文地址:https://www.cnblogs.com/ydymz/p/6445574.html
Copyright © 2020-2023  润新知