struts2实验1:struts2的配置及简单样例运行


layout: post

title: struts2实验1:struts2的配置及简单样例运行

categories: STRUTS2

description: struts2实验1:struts2的配置及简单样例运行

keywords: STRUTS2

p.s
struts2包版本的不同会导致不兼容

github:https://github.com/joeytsai03/javaee_test1.git

运行情况


struts2实验1:struts2的配置及简单样例运行_第1张图片
登陆界面

struts2实验1:struts2的配置及简单样例运行_第2张图片
登陆成功界面

整体的目录结构


struts2实验1:struts2的配置及简单样例运行_第3张图片
目录结构

User代码

package com.zy.javaee.domin;

/**
 * Created by Joey_Tsai on 2018/3/9.
 */
public class User {
    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;
    }

    private String username;
    private String password;

}

LoginAction代码

package com.zy.javaee;

import com.opensymphony.xwork2.ActionSupport;
import com.zy.javaee.domin.User;

/**
 * Created by Joey_Tsai on 2018/3/9.
 */
public class LoginAction extends ActionSupport {
    private User user;

    @Override
    public String execute() throws Exception {
        if (getUser().getUsername().equals("admin") && getUser().getPassword().equals("123")) {
            return SUCCESS;
        } else {
            return LOGIN;
        }
    }

    public User getUser() {
        return user;
    }

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

LoginAction-conversion.properties

user=com.zy.javaee.UserConverter  

LoginPage

package com.zy.javaee;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by Joey_Tsai on 2018/3/9.
 */
public class LoginPage extends ActionSupport {
    @Override
    public String execute() throws Exception {

        return SUCCESS;
    }
}

UserConverter

package com.zy.javaee;

import com.zy.javaee.domin.User;
import ognl.DefaultTypeConverter;

import java.util.Map;

/**
 * Created by Joey_Tsai on 2018/3/9.
 */
public class UserConverter extends DefaultTypeConverter {
    public Object convertValue(Map context, Object value, Class toType){
         /*如果是要转换成User对象*/
        if (toType==User.class) {
            /*页面传递过来的参数被封装成了String[]*/
            String[] params = (String[]) value;
            User user = new User();
            /*取出user字符串,按,分割成数组*/
            String[] uservalues = params[0].split(",");
            user.setUsername(uservalues[0]);
            user.setPassword(uservalues[1]);
            return user;
        }
        /*页面要显示user参数值,则要转换成字符串*/
        else if (toType==String.class) {
            User user =(User)value;
            return "姓名:"+user.getUsername()+", 密码:"+user.getPassword();
        }
        return null;
    }

}

struts.xml




        
        
            /success.jsp
            /index.jsp
        
            
                /KK.jsp
            
        

xwork-conversion.properties

com.zy.javaee.domin.User=com.zy.javaee.UserConverter

web.xml



    
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
        struts2
        /*
    

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    用户登录


用户登录

KK.jsp

<%--
  Created by IntelliJ IDEA.
  User: Joey_Tsai
  Date: 2018/3/9
  Time: 15:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


KK test



success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    登录成功


登录成功!

你可能感兴趣的:(struts2实验1:struts2的配置及简单样例运行)