struts2项目搭建,json应用

1.下载struts-2.3.20-all.zip,并解压

2.创建web项目

3.添加的包有:

4.在web.xml中添加Struts过滤器

<span style="color:#333333;"><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_2_5.xsd" version="2.5"> </span>
<span style="color:#333333;"><session-config>
    <session-timeout>15</session-timeout>
  </session-config>
  <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.jsp</welcome-file>
  </welcome-file-list></span>
5.项目中新建resouce文件夹,添加struts.xml配置

<span style="color:#333333;"><?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.i18n.encoding" value="utf-8" />  
	<package name="login" extends="struts-default">  
		<action name="index" class="com.bonc.action.LoginAction" method="index">
<span style="white-space:pre">			</span><result name="success" type="redirectAction">hdfs</result>
<span style="white-space:pre">			</span><result name="login" type="dispatcher">/view/login.jsp</result>
<span style="white-space:pre">		</span></action>
	</package>  
	<package name="hive" extends="struts-default,json-default">
		<action name="hive" class="com.bonc.action.HiveAction" method="execute">
			<result name="success" type="dispatcher">/view/hive.jsp</result>
		</action>
		
		<action name="add_role" class="com.bonc.action.RoleAction" method="addRole">
			<result name="success" type="redirectAction">
				<param name="actionName">role</param>
				<param name="role_name">${role_name}</param>
			</result>
		</action>
		
	</package>

</struts>
</span>
注意:ddl配置从struts2-core-2.3.20.jar 中的struts-default.xml中复制

6.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title></title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="text/css" href="css/bootstrap.css" rel="stylesheet">

<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

<style type="text/css">
.help-block {
	color: red;
}

#login-div {
	background: #d7d7d7 url(img/bg.png) no-repeat  center;
	height: 386px;
	width: 720px;
	margin: 80px auto 0 auto;
}
#form-div{
	position: relative;
	top: 170px;
}
#login-form{
	width:590px;
	margin:0 auto;
}
#login-btn{
	background: red url(img/bg_09.png) no-repeat  center;
	height:36px;
	margin-left:190px;
	width:93px;
	cursor: pointer;
	border-collapse: separate;
	border-spacing: 1px;
	border-radius:5px;
	
}

</style>

<script type="text/javascript">
	$(document).ready(function() {
		var result = "${result}";
		if ( result == "error") {
			$("#errorName").text("用户名或密码错误!");
		} else {
			$("#errorName").text("");
		}
	});
	
</script>
</head>

<body style="background-color:#d7d7d7 ">
	<div id="login-div">
		<div id="form-div">
			<form class="form-horizontal" method="post" action="login" id="login-form">
				<div class="control-group">
					<label class="control-label"><strong>用户名:</strong></label>
					<div class="controls">
						<input class="input-xlarge" type="text" name="user.name"> 
					</div>
				</div>
				<div class="control-group">
					<label class="control-label"><strong>密    码:</strong></label>
					<div class="controls">
						<input class="input-xlarge" type="password" name="user.passwd">
						<span class="help-block" id="errorName"></span>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
					<div class="row-fluid">
						<div class="span6">
							<input type="checkbox" name="remember"  value="true"/>一周内自动登录
						</div>
						<div class="span5" style="margin-left:25px">
							<input type="submit" class="btn btn-primary" value="登 录">
						</div>
					</div>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div class="container">
		<div class="row-fluid">
			<div class="span6 offset2" style="margin-top:80px;"></div>
		</div>
	</div>
</body>
</html>

7.action要继承ActionSupport

public class LoginAction extends ActionSupport 
	implements SessionAware ,ServletRequestAware, ServletResponseAware{

	private static final long serialVersionUID = 1L;
	public static String USER_SESSION = "user_session";
	
	private LoginService service = new LoginService();
	private CookieUtil cookieUtil = new CookieUtil();
	private User user;
	private String remember;
	private String result;
	private Map<String, Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public String index() {
		return service.index(request);
	}
	
	public String execute(){
		System.out.println(user.getName());
		System.out.println(user.getPasswd());
		user = service.login(user);
		if(user.getId() != 0) {
			System.out.println("login success");
			System.out.println("是否保存Cookie :" + remember);
			
			if(remember != null &&remember.equals("true")) {
				Cookie cookie = cookieUtil.addCookie(user);
				response.addCookie(cookie);
			}
			session.put(USER_SESSION, user);
			return SUCCESS;
		} 
		System.out.println("login error");
		result="error";
		return ERROR;
		
	}
	public String logout() {  
	        HttpSession session = request.getSession(false);  
	        if (session != null)  
	            session.removeAttribute(USER_SESSION);  
	        Cookie cookie = cookieUtil.delCookie(request);  
	        if (cookie != null)  
	            response.addCookie(cookie);  
	        return "login";  
	    }  
}
CookieUtil代码 -----------实现一周内自动登录
<pre name="code" class="java">package com.bonc.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.bonc.action.LoginAction;
import com.bonc.dao.UserDao;
import com.bonc.entity.User;

/**
 * cookie的增加、删除、查询
 */
public class CookieUtil {
	public static final String USER_COOKIE = "user.cookie";

	// 添加一个cookie
	public Cookie addCookie(User user) {
		Cookie cookie = new Cookie(USER_COOKIE, user.getName() + ","
				+ user.getPasswd());
		System.out.println("添加cookie");
		cookie.setMaxAge(60 * 60 * 24 * 7);// cookie保存两周
		return cookie;
	}

	// 得到cookie
	public boolean getCookie(HttpServletRequest request, UserDao userDao) {
		Cookie[] cookies = request.getCookies();
		System.out.println("cookies: " + cookies);
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				System.out.println("cookie: " + cookie.getName());
				if (CookieUtil.USER_COOKIE.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (StringUtils.isNotBlank(value)) {
						String[] split = value.split(",");
						String username = split[0];
						String password = split[1];
						User user = userDao.checkUser(username, password);
						if (user != null) {
							HttpSession session = request.getSession();
							System.out.println("把用户" + user.getName() + " 放进session中");
							session.setAttribute(LoginAction.USER_SESSION, user);// 添加用户到session中
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	// 删除cookie
	public Cookie delCookie(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (USER_COOKIE.equals(cookie.getName())) {
					cookie.setValue("");
					cookie.setMaxAge(0);
					return cookie;
				}
			}
		}
		return null;
	}
}


 

8.ajax请求

$.ajax({
	url : "getTBLS",
	data : {
		'db_id' : id
		},
	type : "post",
	dataType : "json",
	success : function(data) {
	<span style="white-space:pre">	</span>$.each(data.<span style="font-family: Arial, Helvetica, sans-serif;">tblList </span><span style="font-family: Arial, Helvetica, sans-serif;">, function(i, item) {</span>
		<span style="white-space:pre">	</span>
		}
	});
struts.xml文件中配置

	<action name="getTBLS" class="com.bonc.action.HiveAction" method="getTable">
			<result type="json"/>
	</action>
action中实现

	public String getTable() {
		tblList = service.getTablesByDB(tbl_priv.getDb_name());
		System.out.println("table list : " + tblList);
		return SUCCESS;
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值