struts2实验2:struts2.xml action中* ,{}以及${}


layout: post

title: struts2实验2:struts2.xml action中* ,{}以及${}

categories: STRUTS2

description: struts2实验2:struts2.xml action中* ,{}以及${}

keywords: STRUTS2

p.s
关键在于struts.xml中的匹配,使用* ,{}以及${}可以让代码更加简洁清晰

代码核心思路
在struts.xml中通过路由(url)去指定控制逻辑的Action中的方法,例如


        
            ${directUrl}
            testadmin
            123
        
    

中{1}指向login_*中的*部分, 访问/admin/login_adminLoginPage则{1}代表adminLoginPage,method={1},则路由的控制权转至AdminLoginAction.java中的adminLoginPage()方法,其次是${directUrl}指向了AdminLoginAction.jav继承的BaseAction.java中的getDirectUrl(),在AdminLoginAction.java中填充跳转页面路由即可实现由控制器控制跳转页面

普通用户登录 http://localhost:8080/users/login_userLoginPage
管理员用户登录http://localhost:8080/admin/login_adminLoginPage
默认登录 http://localhost:8080/login

支持中文
支持多个扩展名
设置浏览器不缓存静态内容
设置自动重新加载struts.xml

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

运行情况


初始界面

普通用户登陆

普通用户登陆成功

管理员登陆

管理员登陆成功

异常处理

整体的目录结构


目录结构

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;

}

UserLoginAction 代码

package com.zy.javaee.controller.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zy.javaee.controller.BaseAction;
import com.zy.javaee.domin.User;

/**
 * Created by Joey_Tsai on 2018/3/19.
 */
public class UserLoginAction extends BaseAction {
    private User user;
    private User defaultUser;



    public String userLoginPage(){
        setDirectUrl("/html/userLoginPage.jsp");
        return SUCCESS;
    }
    public String login(){
        if (getUser().getUsername().equals(getDefaultUser().getUsername()) && getUser().getPassword().equals(getDefaultUser().getPassword())) {
            ActionContext.getContext().getSession().put("user",user);
            setDirectUrl("/html/helloPage.jsp");
            return SUCCESS;
        } else {

            return userLoginPage();
        }
    }

    public User getUser() {
        return user;
    }

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

    public User getDefaultUser() {
        return defaultUser;
    }

    public void setDefaultUser(User defaultUser) {
        this.defaultUser = defaultUser;
    }


}

LoginAction-conversion.properties

user=com.zy.javaee.UserConverter  

userLoginPage

  Created by IntelliJ IDEA.
  User: Joey_Tsai
  Date: 2018/3/19
  Time: 14:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    普通用户登陆界面



普通用户登陆
管理员登陆

普通用户登陆

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




    
    
    
    

    
        
            /error.jsp
        
        
            
            
        
        
            /users/login_userLoginPage.do
        
    
    
        
            /users/login_userLoginPage.action
        
    
    
        
            ${directUrl}
            testadmin
            123
        
    

    
        
            ${directUrl}
            testuser
            123
        
    

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"%>


    实验2


普通用户登陆
管理员登陆



error.jsp

<%--
  Created by IntelliJ IDEA.
  User: Joey_Tsai
  Date: 2018/3/19
  Time: 17:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    页面错误




<%----%>
<%----%>




helloPage.jsp

<%--
  Created by IntelliJ IDEA.
  User: Joey_Tsai
  Date: 2018/3/19
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    欢迎页面


普通用户登陆
管理员登陆

登陆成功!!!!

adminLoginPage.jsp

<%--
  Created by IntelliJ IDEA.
  User: Joey_Tsai
  Date: 2018/3/19
  Time: 14:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    管理员登陆界面


普通用户登陆
管理员登陆


管理员登陆

AdminLoginAction

<%--
  Created by IntelliJ IDEA.
  User: Joey_Tsai
  Date: 2018/3/19
  Time: 14:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>


    管理员登陆界面


普通用户登陆
管理员登陆


管理员登陆

ErrorProcessAction

package com.zy.javaee.controller.actions;

import com.opensymphony.xwork2.ActionContext;
import com.zy.javaee.controller.BaseAction;
import com.zy.javaee.domin.User;

import java.sql.SQLException;

/**
 * Created by Joey_Tsai on 2018/3/19.
 */
public class ErrorProcessAction extends BaseAction {

    public String tryException() throws Exception {

            throw new Exception();
    }

}

BaseAction

package com.zy.javaee.controller;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by Joey_Tsai on 2018/3/19.
 */
public abstract class BaseAction extends ActionSupport{
    private String directUrl;

    public String getDirectUrl() {
        return directUrl;
    }

    public void setDirectUrl(String directUrl) {
        this.directUrl = directUrl;
    }
}

你可能感兴趣的:(struts2实验2:struts2.xml action中* ,{}以及${})