首页 > 代码库 > struts2学习(2)struts2核心知识

struts2学习(2)struts2核心知识

一、Struts2 get/set 自动获取/设置数据

根据上一章。中的源码继续。

HelloWorldAction.java中private String name,自动获取/设置name:

package com.cy.action;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action{
    
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String execute() throws Exception {
        System.out.println("执行了Action的默认方法");
        return SUCCESS;
    }

}

helloWorld.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${name }你好!!
</body>
</html>

1)使用get方式:

浏览器中访问:http://localhost:8080/Struts2Chap01/hello?name=asdasd

结果:

技术分享

2)post方式:

编写index.jsp页面:

<body>
    <form action="hello" method="post">
        name: <input type="text" name="name"/>
        <input type="submit" value="submit" />
    </form>
</body>

浏览器中访问:http://localhost:8080/Struts2Chap01/index.jsp,输入name:

技术分享

点击submit:

技术分享

 

二、ActionSupport 类引入

HelloWorldAction2.java中继承ActionSupport:

package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction2 extends ActionSupport{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String execute() throws Exception {
        System.out.println("执行了HelloWorldAction2 Action的默认方法");
        return SUCCESS;
    }

}

struts.xml中修改为com.cy.action.HelloWorldAction2:

<?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来区分 -->
    <package name="helloWorld" extends="struts-default">
        <action name="hello" class="com.cy.action.HelloWorldAction2">
            <!-- 默认是转发,转发到helloWorld.jsp -->
            <result name="success">helloWorld.jsp</result>
        </action>
    </package>

</struts>

测试结果ok;

ActionSupport功能更强大,以后都用ActionSupport。
 
 
三、Action 设置数据
 技术分享
需求:我们模拟用户登录,如果用户名为cy,密码为123456就登录成功,否则失败; 
第一种:属性驱动:
A.基本数据类型属性:
com.cy.action.UserAction.java:
技术分享
package com.cy.action;

import com.cy.model.User;
import com.cy.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
    
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserService();
    private String userName;
    private String password;
    
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String execute() throws Exception {
        System.out.println("执行了UserAction Action的默认方法");
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        if(userService.login(user)){
            return SUCCESS;
        }else{
            return ERROR;
        }
        
    }

}
View Code

com.cy.model.User:

技术分享
package com.cy.model;

public class User {
    private String userName;
    private String password;
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
View Code

com.cy.service.UserService:

技术分享
package com.cy.service;

import com.cy.model.User;

public class UserService {
    
    public boolean login(User user){
        if("cy".equals(user.getUserName()) && "123456".equals(user.getPassword())){
            return true;
        }else{
            return false;
        }
    }
}
View Code

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>

    <!-- 可以创建很多的package,用name来区分 -->
    <package name="helloWorld" extends="struts-default">
        <action name="hello" class="com.cy.action.HelloWorldAction2">
            <!-- 默认是转发,转发到helloWorld.jsp -->
            <result name="success">helloWorld.jsp</result>
        </action>
        
        <action name="user" class="com.cy.action.UserAction">
            <result name="success">success.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>

</struts>

login.jsp登录界面:

<body>
    <form action="user" method="post">
        用户名: <input type="text" name="userName"/><br>
        密码: <input type="password" name="password"/><br>
        <input type="submit" value="登录" />
    </form>
</body>

success.jsp登录成功的页面:

<body>
    登录成功!
</body>

error.jsp登录失败的界面:

技术分享
<body>
    登录失败!
</body>
View Code

测试:

浏览器访问http://localhost:8080/Struts2Chap01/login.jsp,输入cy,123456

技术分享

登录成功:

技术分享

 

B:javaBean类型属性驱动:

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
----------

 

struts2学习(2)struts2核心知识