Struts2简单的使用

Struts2主要就是利用拦截器来实现

先铺前台代码

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

<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.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">
    -->

  </head>

  <body>
  方式一  访问实现Action  直接通过属性接收值
   <form action="index1.action" method="post" >
   用户名:<input type="text" name="name"> <br>
  密码:<input type="text" name="pwd"> <br>
  <input type="submit" value="登录">
   </form>

    方式二 访问方法execute  尝试用对象的方式传值
   <form action="index2.action" method="post" >
   用户名:<input type="text" name="u.name"> <br>
  密码:<input type="text" name="u.pwd"> <br>
  <input type="submit" value="登录">
   </form>
   方式三 访问继承类ActionSupport
   <form action="index3.action" method="post" >
   用户名:<input type="text" name="name"> <br>
  密码:<input type="text" name="pwd"> <br>
  <input type="submit" value="登录">
   </form> 

尝试使用对象数组方式传值 <br>
下面以对象集合的方式提交数据
   <form action="index3.action" method="post" >
   用户名:<input type="text" name="users[0].name"> <br>
  密码:<input type="text" name="users[0].pwd"> <br>

   用户名:<input type="text" name="users[1].name"> <br>
  密码:<input type="text" name="users[1].pwd"> <br>

   用户名:<input type="text" name="users[2].name"> <br>
  密码:<input type="text" name="users[2].pwd"> <br>
  <input type="submit" value="提交">
   </form>  
  </body>
</html>

web.xml配置

<?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>classStrutsAction</display-name>
  <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>

Struts2Action/动作

方式一
package com;

import com.opensymphony.xwork2.Action;
/**
 * 方式一  实现Action接口  重写excuse方法
 * 示范:以属性的方式接收前台传来的值
 * @date 2017年9月5日上午10:00:17
 */
public class TestStrutsOne implements Action {

    private String name;
    private String pwd;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String execute() throws Exception {

        System.out.println("this is implements Action");
        System.out.println("name "+name+"   pwd"+pwd);
        return "ok";
    }

}
方式二
package com;

import com.entity.User;

/**
 * 方式二  直接重写  execute方法
 * 示范使用对象的方式向前台取值
 *
 * @date 2017年9月5日上午10:01:43
 */
public class TestStrutsTwe {
    private User u;//对应前台的name     u.name   u.pwd


    public User getU() {
        return u;
    }


    public void setU(User u) {
        this.u = u;
    }


    public String execute(){
        System.out.println("this is execute");
        System.out.println("username:"+u.getName()+"  userpwd:"+u.getPwd());
        return "ok";
    }
}
方式三
package com;

import java.util.List;

import com.entity.User;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 方法三  继承 ActionSupport
 * 推荐使用  提供了很多其它的方法
 * 示范使用集合方式向前台取值
 *
 * @date 2017年9月5日上午10:04:43
 */
public class TestStrutsThree extends ActionSupport {

    private List<User> users;//对应前台的name   users[0].neme  users[1].pwd


    public List<User> getUsers() {
        return users;
    }


    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String execute() throws Exception {
        System.out.println("this is extends ActionSupport");
        System.out.println("users1 :"+users.get(0).getName()+"///users2 :"+users.get(1).getName());
        return "ok";
    }


}
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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" extends="struts-default">
        <action name="index" class="action.LoginStruts">
        <!-- action name(index)为浏览器访问地址  class为action类的路径 -->
            <result name="ok">/index.jsp</result>
            <!-- result name为action类返回值     /index.jsp跳转页面-->
        </action>

        <action name="index1" class="com.TestStrutsOne">

            <result name="ok">/index.jsp</result>

        </action>

        <action name="index2" class="com.TestStrutsTwe">
            <result name="ok">/index.jsp</result>
        </action>

        <action name="index3" class="com.TestStrutsThree">
            <result name="ok">/index.jsp</result>
        </action>


    </package>



    <!-- Add packages here -->

</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值