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中的方法,例如
<package name="admin" namespace="/admin" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.AdminLoginAction" method="{1}">
<result name="success">${directUrl}</result>
<param name="defaultAdmin.username">testadmin</param>
<param name="defaultAdmin.password">123</param>
</action>
</package>
中{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
<constant name="struts.i18n.encoding" value=“GBK"/> 支持中文
<constant name="struts.action.extension" value="do,action,"/> 支持多个扩展名
<constant name="struts.serve.static.browserCache" value="false"/> 设置浏览器不缓存静态内容
<constant name="struts.configuration.xml.reload" value="true"/> 设置自动重新加载struts.xml
运行情况
初始界面
普通用户登陆
普通用户登陆成功
管理员登陆
管理员登陆成功
异常处理
整体的目录结构
目录结构
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"%>
<html>
<head>
<title>普通用户登陆界面</title>
</head>
<body>
<a href="/login">普通用户登陆</a>
<a href="/admin/login_adminLoginPage">管理员登陆</a>
<h1>普通用户登陆</h1>
<s:form action="/users/login_login" method="POST">
<table>
<tr>
<td><s:textfield name="user.username" label="用户名" value="testuser"/>
</td>
</tr>
<tr>
<td><s:password name="user.password" label="密码" value="123"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><s:submit value="登录"/></td>
</tr>
</table>
</s:form>
</body>
</html>
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.action.extension" value="do,action,"/>
<constant name="struts.serve.static.browserCache" value="false"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="exception" >/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="tryException" class="com.zy.javaee.controller.actions.ErrorProcessAction" method="tryException">
<result name="success">/users/login_userLoginPage.do</result>
</action>
</package>
<package name="default" namespace="/" extends="struts-global">
<action name="login">
<result type="redirect">/users/login_userLoginPage.action</result>
</action>
</package>
<package name="admin" namespace="/admin" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.AdminLoginAction" method="{1}">
<result name="success">${directUrl}</result>
<param name="defaultAdmin.username">testadmin</param>
<param name="defaultAdmin.password">123</param>
</action>
</package>
<package name="users" namespace="/users" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.UserLoginAction" method="{1}">
<result name="success">${directUrl}</result>
<param name="defaultUser.username">testuser</param>
<param name="defaultUser.password">123</param>
</action>
</package>
</struts>
xwork-conversion.properties
com.zy.javaee.domin.User=com.zy.javaee.UserConverter
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>实验2</title>
</head>
<body>
<a href="/login">普通用户登陆</a>
<a href="/admin/login_adminLoginPage">管理员登陆</a>
</body>
</html>
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"%>
<html>
<head>
<title>页面错误</title>
</head>
<body>
<%--<s:property value="exception.message"/>--%>
<%--<s:property value="exceptionStack"/>--%>
<s:debug></s:debug>
</body>
</html>
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"%>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
<a href="/login">普通用户登陆</a>
<a href="/admin/login_adminLoginPage">管理员登陆</a>
<h1><s:property value="user.username"/></h1>
登陆成功!!!!
</body>
</html>
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"%>
<html>
<head>
<title>管理员登陆界面</title>
</head>
<body>
<a href="/login">普通用户登陆</a>
<a href="/admin/login_adminLoginPage">管理员登陆</a>
<h1>管理员登陆</h1>
<s:form action="/admin/login_login" method="POST">
<table>
<tr>
<td><s:textfield name="user.username" label="用户名" value="testadmin"/>
</td>
</tr>
<tr>
<td><s:password name="user.password" label="密码" value="123"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><s:submit value="登录"/></td>
</tr>
</table>
</s:form>
</body>
</html>
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"%>
<html>
<head>
<title>管理员登陆界面</title>
</head>
<body>
<a href="/login">普通用户登陆</a>
<a href="/admin/login_adminLoginPage">管理员登陆</a>
<h1>管理员登陆</h1>
<s:form action="/admin/login_login" method="POST">
<table>
<tr>
<td><s:textfield name="user.username" label="用户名" value="testadmin"/>
</td>
</tr>
<tr>
<td><s:password name="user.password" label="密码" value="123"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><s:submit value="登录"/></td>
</tr>
</table>
</s:form>
</body>
</html>
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;
}
}