项目预览
用到JAR包
实现代码
①/day_0523_01/src/struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="userAction" namespace="/loginCheck" extends="struts-default">
<action name="login" class="cn.edu.imut.action.UserAction" method="checkUser">
<result name="success" type="dispatcher">success.jsp</result>
<result name="error" type="redirect">error.jsp</result>
<result name="input" type="dispatcher">loginView.jsp</result>
</action>
</package>
</struts>
②/day_0523_01/src/cn/edu/imut/action/UserAction.java
package cn.edu.imut.action;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.edu.imut.beans.User;
/**
* @author ZhuXueLiang
*/
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -3524525378604293407L;
private User user = new User();
public String checkUser() {
Map<String, Object> session = ActionContext.getContext().getSession();
//超级简单验证
if ("朱学良".equals(user.getUserName()) && "admin123".equals(user.getUserPass())) {
this.dealFile();
session.put("message", user.getUserName() + ",欢迎你!");
return SUCCESS;
}
session.put("message", user.getUserName() + "不存在!");
return ERROR;
}
/**
* 上传文件
*/
public void dealFile() {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (user.getUserFile() != null) {
File target = new File(realpath, user.getUserFileFileName());
//如果文件不存在,就创建
if (!target.getParentFile().exists())
target.getParentFile().mkdirs();
try {
FileUtils.copyFile(user.getUserFile(), target);
} catch (IOException e) {
e.printStackTrace();
}
// user.getUserFile().renameTo(target);
}
}
public void validate() {
if ("".equals(user.getUserName()) || user.getUserName() == null)
this.addFieldError("user.userName", "用户名不能为空");
if ("".equals(user.getUserPass()) || user.getUserPass() == null)
this.addFieldError("user.userPass", "密码不能为空");
if ("".equals(user.getAddress()) || user.getAddress() == null)
this.addFieldError("user.address", "地址不能为空");
if (user.getUserFile() == null)
this.addFieldError("user.userFile", "文件不能为空");
this.addActionMessage("提交失败!");
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
③/day_0523_01/src/cn/edu/imut/beans/User.java
package cn.edu.imut.beans;
import java.io.File;
public class User {
private String userName;
private String userPass;
private String address;
private File userFile;
private String userFileFileName;
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public File getUserFile() {
return userFile;
}
public void setUserFile(File userFile) {
this.userFile = userFile;
}
public String getUserFileFileName() {
return userFileFileName;
}
public void setUserFileFileName(String userFileFileName) {
this.userFileFileName = userFileFileName;
}
}
④/day_0523_01/WebContent/loginCheck/loginView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h3>登录页面</h3>
<s:form namespace="/loginCheck" action="login.action" enctype="multipart/form-data" method="post">
<s:textfield name="user.userName" label="用户名"/>
<s:password name="user.userPass" label="密码"/>
<s:select name="user.address" label="住址" headerKey=""
headerValue="请选择住址" list="#{'金川':'金川校区','新城':'新城校区','盛乐':'盛乐校区'}"/>
<s:file name="user.userFile" class="form-control" label="上传头像"/>
<s:submit value="提交"/>
</s:form>
<s:actionmessage/>
</body>
</html>
⑤/day_0523_01/WebContent/loginCheck/error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>失败页面</title>
</head>
<body>
<s:property value="#session.message" /><br>
<a href="<s:url value="loginView.jsp"/>">返回重新登录</a>
</body>
</html>
⑥/day_0523_01/WebContent/loginCheck/success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>成功页面</title>
<style type="text/css">
img{
border-radius: 50%;
border:1px solid #000;
width:100px;
height: 100px;
}
div{
margin:auto;
padding:30px;
border:1px solid #000;
width:300px;
line-height: 30px;
background-color: buttonhighlight;
}
body{
position: relative;
}
</style>
</head>
<body>
<div>
<s:property value="#session.message" />
<img src='<s:url value="/images/%{user.userFileFileName}"/>' alt="IMAGE?"/><br>
用户名:<s:property value="%{user.userName}" /><br>
密码:<s:property value="%{user.userPass}" /><br>
地址:工业大学(<s:property value="%{user.address}" />校区)<br>
</div>
</body>
</html>
⑦/day_0523_01/WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>day_0523_01</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
结果显示
优点与不足
刚学Struts2,只是练练手,如有问题,请多指教。
项目源码:点击打开链接