layout: post
title: struts2实验1:struts2的配置及简单样例运行
categories: STRUTS2
description: struts2实验1:struts2的配置及简单样例运行
keywords: STRUTS2
p.s
struts2包版本的不同会导致不兼容
运行情况
登陆界面
登陆成功界面
整体的目录结构
目录结构
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
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="Login" class="com.zy.javaee.LoginAction">
<result name="success">/success.jsp</result>
<result name="login">/index.jsp</result>
</action>
<action name="LoginPage" class="com.zy.javaee.LoginPage">
<result name="success">/KK.jsp</result>
</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>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<s:form action="Login" method="POST">
<table>
<tr>
<td><s:textfield name="user.username" label="用户名"/>
</td>
</tr>
<tr>
<td><s:password name="user.password" label="密码"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><s:submit value="登录"/></td>
</tr>
</table>
</s:form>
</body>
</html>
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" %>
<html>
<head>
<title>Title</title>
</head>
<body>
KK test
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h1><s:property value="user.username"/></h1>
<h4>登录成功!</h4>
</body>
</html>
网友评论