struts入门实例

Posted 风起时只有我在

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts入门实例相关的知识,希望对你有一定的参考价值。

创建maven web项目

配置pom.xml 加入struts的依赖包

<dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.13</version>
    </dependency>

下面是pom.xml的全部配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ssz</groupId>
  <artifactId>Demo03</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Demo03 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.18</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!--jsp  -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.13</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>${project.artifactId}</finalName>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <!-- 处理无法加载资源配置文件 -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>

在src/main/resources/struts.xml  建立struts核心配置文件 配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.i18n.reload" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.custom.i18n.resources" value="res"/>
    <constant name="struts.action.extension" value="action,,do"/>
    <package name="default" namespace="/" extends="struts-default">
        <action name="login">
            <result>/login.jsp</result>
        </action>
        <action name="test" class="com.ssz.util.Test" method="tt"></action>
        <action name="ts" class="com.ssz.util.Test" method="aa"></action>
        <action name="login" class="com.ssz.util.User">
            <result>/welcome.jsp</result>
            <result name="fail">/error.jsp</result>
        </action>
    </package>
</struts>

编写控制器测试类Test.java

package com.ssz.util;

import lombok.Data;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Data
public class Test {
    private String name;
    public void tt(){
        System.out.println("Hello-World struts"+name);
    }
    public void aa() throws IOException {
        HttpServletResponse res= ServletActionContext.getResponse();
        res.setCharacterEncoding("utf-8");
        res.setContentType("text/html,charset=utf-8");
        res.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
        PrintWriter out = res.getWriter();
        out.print("<h3>fqszywz</h3>"+name);
        out.flush();
        out.close();
    }
}

User.java 登录验证

package com.ssz.util;

import com.sun.net.httpserver.Authenticator;
import lombok.Data;

@Data
public class User {
    private String name;
    private String password;
    public String execute(){
        String result="success";
        if(!getName().equals("admin")||!getPassword().equals("123")){
            result ="fail";
        }
        return result;
    }
}

编写index.jsp 文件内容,进行测试

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>
<body>
<form action="login" method="post">
    <table>
        <tr>
            <td>账户</td>
            <td><input type="name" name="name" autofocus></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password"  name="password"></td>
        </tr>
        <tr>
            <td><input type="submit" value="登陆"></td>
        </tr>
    </table>
</form>
<hr>
<s:iterator begin="1" var="vv" end="10">
    <h4>${vv}</h4>
</s:iterator>
<h3><a href="test"/>test</h3>
<h3><a href="ts"/>ts</h3>
</body>
</html>

登陆成功跳转welcome.jsp页面

<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>FQSZYWZ</title>
</head>
<body>
<h3>会员登陆</h3>
<h3>${name}登陆成功</h3>
</body>
</html>

登陆失败页面error.jsp页面

<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>FQSZYWZ</title>
</head>
<body>
<h3>会员登陆</h3>
<h3>登陆失败,请检查用户名密码重新登录</h3>
<h4><a href="login.jsp"/> </h4>
</body>
</html>

 

以上是关于struts入门实例的主要内容,如果未能解决你的问题,请参考以下文章

struts入门实例

Struts2的一个入门实例----登录功能

java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器

Struts2学习资料

Struts简单入门

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段