struts2 demo

1. 新建web工程:web_ssm
2. 导入jar包:
struts2相关jar包

3. web.xml

<!-- 配置struts2 -->
  <filter>
    <filter-name>struts</filter-name>
    <filter-class>      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

4.java(仅仅给出action)

package com.login.action;



import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;


/**
 * 登陆鉴权Action类
 * 
 * @author fanjm
 * @version 1.0
 * @since 2017年02月02日
 */
public class LoginAction extends  ActionSupport implements RequestAware,SessionAware, ApplicationAware{

    private static final long serialVersionUID = 2408145375650667872L;
    /** log */                
    /*private static final Logger logger = LoggerFactory
            .getLogger(LoginAction.class);*/
    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;

    private String username;
    private String pwd;

    public String login(){
        String rtn = ERROR;
        if("admin".equals(username)&&"admin".equals(pwd)){
            rtn = SUCCESS;
        }
        return rtn;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public String getPwd() {
        return pwd;
    }


    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


    public Map<String, Object> getRequest() {
        return request;
    }
    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }
    public Map<String, Object> getSession() {
        return session;
    }
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
    public Map<String, Object> getApplication() {
        return application;
    }
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }

}

5.1 struts文件目录结构
这里写图片描述

5.2 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>
    <constant name="struts.i18n.encoding" value="GBK"></constant>

   <constant name="struts.enable.DynamicMethodInvocation" value="true" />
   <constant name="struts.devMode" value="true" />

   <include file="struts-config/struts_login.xml" />
<!-- Add packages here -->

</struts>

5.3 struts_login.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="/as" extends="struts-default">
        <!-- 
            自定义拦截器栈

        <interceptors>
            <interceptor-stack name="timertokenstack">
                <interceptor-ref name="defaultStack"/>
                <interceptor-ref name="timer"/>
                <interceptor-ref name="auth"/>
            </interceptor-stack>
            <interceptor name="auth" class="com.interceptor.AuthInterceptor"></interceptor>
        </interceptors>
        -->
        <global-results>
            <result name="error">/WEB-INF/jsp/fail.jsp</result>
        </global-results>

        <action name="login" class="com.login.action.LoginAction">
            <result name="success">/WEB-INF/jsp/success.jsp</result>
        </action>
    </package>
</struts>

6. index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
    $("#submit").click(function(){
        $("#form1").attr("action","<%=basePath%>as/login!login.action");
        $("#form1").submit();
    });
});
</script>
  </head>

  <body>
    This is my JSP page. <br>
    <div id='max'>
        <form id="form1" name="form1">
            <input type="text" name="username" />
            <input type="password" name="pwd" />
            <button id="submit">submit</button>
        </form>
    </div>
  </body>
</html>

注:<base href="<%=basePath%>">$("#form1").attr("action","<%=basePath%>as/login!login.action");不起作用。

7.整体目录结构
这里写图片描述

以下是一个简单的 Struts2 示例,将输入的名称传递给 Action 类并在 JSP 页面上显示欢迎消息。 首先,我们需要创建一个名为 "WelcomeAction" 的 Action 类,在其中定义一个名为 "name" 的属性和一个名为 "execute" 的方法。 ```java package com.example.struts2demo; public class WelcomeAction { private String name; public String execute() { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` 接下来,我们需要创建一个名为 "welcome.jsp" 的 JSP 页面,该页面将显示欢迎消息,并使用 Struts2 标记库获取 Action 类中设置的名称。 ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome <s:property value="name" /></h1> </body> </html> ``` 接下来,我们需要创建一个名为 "struts.xml" 的配置文件,该文件将指定如何将请求映射到 Action 类以及如何处理结果。 ```xml <?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> <package name="default" extends="struts-default"> <action name="welcome" class="com.example.struts2demo.WelcomeAction"> <result name="success">/welcome.jsp</result> </action> </package> </struts> ``` 最后,我们需要创建一个名为 "index.jsp" 的 JSP 页面,该页面将显示一个表单,允许用户输入他们的名称,并将其作为参数传递给 Action 类。 ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome</h1> <s:form action="welcome"> <s:textfield name="name" label="Enter your name" /> <s:submit value="Submit" /> </s:form> </body> </html> ``` 现在,我们已经完成了 Struts2 示例的所有必要组件。要运行示例,请将这些文件保存在您的 Web 应用程序中,并使用您喜欢的 Web 服务器启动应用程序。然后,导航到 "index.jsp" 页面,输入您的名称并单击提交按钮,应该会显示一个欢迎消息,其中包含您输入的名称。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值