Struts2基本框架搭建

1.首先需要登录Apache官网下载和安装Struts2框架
网址:https://struts.apache.org/download.cgi 里边有多个版本,推荐下载:Full Distribution版本。
2.将下载的压缩文件解压,然后再将解压后的Apps中的struts2-blank.war继续解压
然后在路径:apps\struts2-blank\WEB-INF\lib中有13个压缩文件
3.搭载Struts环境
示例:
这里写图片描述

       (2)在src根目录下新建struts.xml文件,然后将:struts-2.3.31-all\struts-2.3.31\apps\struts2-blank\WEB-INF\src\java路径下的struts.xml文件内容粘到新建的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="default" namespace="/" extends="struts-default">
        <action name="login" class="com.iotek.action.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
         <result name="success">/WEBINF/content/welcome.jsp</result>     
        </action>
    </package>
</struts>
    (3)d)在web.xml中添加struts2的过滤器
    示例:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>StrutsKetang</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.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.最后完成页面和action的编写
为了避免其他人员知道其他页面路径后跳过过滤器,直接访问页面,所有一般将其他页面放在WEB-INF/content中(若没有content文件夹,自己创建一个)
index.jsp代码示例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login">//这里的login就是struts.xml中定义的action的name
    姓名:<input type="text" name="name"><br>
   密码:<input type="password" name="password"><br>
   <input type="submit" name="submit" value="提交">
   <input type="reset" value="取消">
</form>
</body>
</html>

LoginAction.java代码示例:

package com.iotek.action;

public class LoginAction {
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String execute(){
        if(getName().equals("xiaoming")&&getPassword().equals("123456")){
            System.out.println("登录成功");
            return "success";
        }else{
            return "error"; 
        }

    }

}

welcome.jsp或error.jsp代码示例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name }
${password }
<p>登录成功</p>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值