关闭

简单strus2项目的搭建

67人阅读 评论(0) 收藏 举报
分类:

1.新建一个Dynamic Web Project,项目名称strtus2Demo,项目架构预览





2.导入struts2相关jar包,jar包列表

(1)commons-logging:用来记录程序运行时的活动的日志

(2)freemarker:一个模板引擎,视图组件,用来生成web页面

(3)ognl:el表达式,jsp脚本的替代品,例如jsp中的<%%>

(4)struts2-core

(5)xwork


3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts2Demo</display-name>
  
  <!-- struts的配置 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 欢迎页的配置 -->
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4.用户类,UserBean.java

package hen.struts.bean;

/**
 * 用户实体类
 * 
 * @author huaernan
 * 
 */
public class UserBean {

	private String username;

	private String password;

	private String realname;

	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;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

}

用户接口类,UserDao.java

package hen.struts.dao;

import hen.struts.bean.UserBean;

/**
 * 用户操作接口
 * 
 * @author huaernan
 * 
 */
public interface UserDao {

	/**
	 * 根据账号密码登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public UserBean userLogin(String username, String password);

	/**
	 * 用户注册
	 * 
	 * @param userBean
	 * @return
	 */
	public boolean userRegister(UserBean userBean);

}

接口实现类,UserDaoImpl.java

package hen.struts.daoImpl;

import hen.struts.bean.UserBean;
import hen.struts.dao.UserDao;

/**
 * 用户接口实现类
 * 
 * @author huaernan
 * 
 */
public class UserDaoImpl implements UserDao {

	/**
	 * 用户登录
	 */
	@Override
	public UserBean userLogin(String username, String password) {
		UserBean userBean = null;
		// 这里写死的,本来应该查询数据库来返回对象的
		if (username.equals("admin") && password.equals("admin")) {
			userBean = new UserBean();
			userBean.setRealname("管理员");
		}
		return userBean;
	}

	/**
	 * 用户注册
	 */
	@Override
	public boolean userRegister(UserBean userBean) {
		boolean result = false;
		// 这里应该是执行数据库插入,这里简单写,判断传来的值不空就代表插入数据成功,返回true
		if (userBean.getPassword() != null && userBean.getRealname() != null
				& userBean.getRealname() != null) {
			result = true;
		}
		return result;
	}

}

action类,继承自ActionSupport,LoginAction.java

package hen.struts.action;

import hen.struts.bean.UserBean;
import hen.struts.dao.UserDao;
import hen.struts.daoImpl.UserDaoImpl;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {

	// 用户bean,要设置get、set方法通过struts将表单属性名称和userBean的属性名称赋值
	private UserBean userBean;

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	// 这里直接new,丰富的话是可以使用spring的依赖注释管理
	private UserDao userDao = new UserDaoImpl();

	/**
	 * 用户登录
	 * 
	 * @return
	 */
	public String login() {
		String username = userBean.getUsername();
		String password = userBean.getPassword();
		userBean = userDao.userLogin(username, password);
		if (userBean != null) {
			return SUCCESS;
		} else {
			return ERROR;
		}

	}

	/**
	 * 用户注册
	 * 
	 * @return
	 */
	public String userRegister() {

		boolean result = userDao.userRegister(userBean);
		if (result) {
			return SUCCESS;
		} else {
			return ERROR;
		}

	}

}

5.struts.xml文件,此文件的包要Build Path——Use As Resource Folder,也就是放到classpath下面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2Demo" extends="struts-default">
<!-- 用户登录  根据name:login,找到class中对应的方法名称-->
<action name="dologin" class="hen.struts.action.LoginAction" method="login">
<result name="success">/welcome.jsp</result>
</action>

<!-- 用户注册 -->
<action name="doRegister" class="hen.struts.action.LoginAction" method="userRegister">
<result name="success">/welcome.jsp</result>
</action>

</package>
</struts>

6.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<form action="dologin.action" method="post">
登录账号:<input type="text" name="userBean.username"><br/>
登录密码:<input type="password" name="userBean.password"><br/>
<input type="submit" value="登录"> <input type="button" onclick="enterRegisterPage()" value="注册">
</form>

<!-- js打开注册页面 -->
<script type="text/javascript">
function enterRegisterPage(){
	window.open("userRegister.jsp");
}
</script>

</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>欢迎-struts项目欢迎页</title>
</head>
<body>
<h1>欢迎您,${userBean.realname}</h1>
</body>
</html>

userRegister.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<form action="doRegister.action" method="post">
登录账号:<input type="text" name="userBean.username"><br/>
登录密码:<input type="text" name="userBean.password"><br/>
真实姓名:<input type="text" name="userBean.realname"><br/>
<input type="submit" value="确定">
<input type="reset" value="重置">
</form>
</body>
</html>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章存档