struts2学习struts2核心知识

Posted 有点懒惰的大青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2学习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类型属性驱动:

 将上面的UserAction中改为private User user;javaBean的方式:

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 User user;
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String execute() throws Exception {
        System.out.println("执行了UserAction Action的默认方法 采用javaBean");
        if(userService.login(user)){
            return SUCCESS;
        }else{
            return ERROR;
        }
        
    }

}
View Code

login.jsp登录界面需要修改为如下:

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

测试结果ok

 

第二种:模型驱动:

UserAction.java: //主要实现ModelDriven<User>, private User user = new User(),这里要new一个user:

package com.cy.action;

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

public class UserAction extends ActionSupport implements ModelDriven<User>{
    
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserService();
    private User user = new User();

    public String execute() throws Exception {
        System.out.println("执行了UserAction Action的默认方法 采用模型驱动");
        if(userService.login(user)){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }

    public User getModel() {
        return user;
    }

}

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>
ModelDriven的缺点是:只接受一个javaBean;
 
测试结果ok

 

 

四、Struts2处理传入多个值                                          

1、需求:选择多个复选框,将复选框选中的值传入到后台;

2、需求:前端批量添加多个Student到后台,后台接受

HobbyAction.java:

package com.cy.action;

import com.opensymphony.xwork2.Action;

public class HobbyAction implements Action{
    
    private String[] hobby;
    
    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

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

}
View Code

StudentAction.java:

package com.cy.action;

import java.util.List;

import com.cy.model.Student;
import com.opensymphony.xwork2.Action;

public class StudentAction implements Action{
    
    private List<Student> students;
    
    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public String execute() throws Exception {
        System.out.println("执行了StudentAction的默认方法");
        for(Student s : students){
            System.out.println(s);
        }
        return SUCCESS;
    }

}
View Code

com.cy.model.Student.java:

package com.cy.model;

public class Student {

    private String name;
    private String sex;
    private int age;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }

    
    
}
View Code

struts.xml配置:

<struts>

    <package name="helloWorld" extends="struts-default">
        <action name="hobby" class="com.cy.action.HobbyAction">
            <result name="success">success.jsp</result>
        </action>
        
        <action name="student" class="com.cy.action.StudentAction">
            <result name="success">success.jsp</result>
        </action>
    </package>

</struts>
View Code

hobby.jsp:

<body>
<form action="hobby" method="post">
    爱好:
    <input type="checkbox" name="hobby" value="唱歌"/>唱歌
    <input type="checkbox" name="hobby" value="跳舞"/>跳舞
    <input type="checkbox" name="hobby" value="睡觉"/>睡觉
    <input type="checkbox" name="hobby" value="玩CF"/>玩CF
    <input type="submit" value="提交"/>
</form>
</body>

addstudents.jsp:

<body>
<form action="student" method="post">
    <table>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
        </tr>
        <tr>
            <!-- 这边students[0].name命名有规范的,不然action中取不到 -->
            <td><input type="text" name="students[0].name"/></td>
            <td><input type="text" name="students[0].sex"/></td>
            <td><input type="text" name="students[0].age"/></td>
        </tr>
        <tr>
            <td><input type="text" name="students[1].name"/></td>
            <td><input type="text" name="students[1].sex"/></td>
            <td><input type="text" name="students[1].age"/></td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="submit" value="提交"/>
            </td>
        </tr>
    </table>
</form>
</body>

success.jsp:

<body>
    OK!
</body>

测试结果:

1)

 

 

2)

 

以上是关于struts2学习struts2核心知识的主要内容,如果未能解决你的问题,请参考以下文章

Struts2——核心知识

Struts2 核心知识

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

SSH学习笔记用Struts2实现简单的用户登录

struts2自定义拦截器

Struts2学习