手把手教你搭建ssh框架(代码拿过来就能用)

jdk的环境变量的配置这里就不说了  

还有你使用的ide  无论是什么都可以

如歌以myeclipese为例

1:咱们先做的是struts2

第一步当然是下载struts2的jar包啦

Struts 2.5.10.1

以及struts2的maven依赖

<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.10.1</version>
</dependency>

接下来按照如歌思路贴出相应的代码

1》index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <a href="login.jsp">登录</a>
  </body>
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <form method="post" action="login1">
 请输入账号:<input type="text" name="username" value=""/><br />
 请输入密码:<input type="password" name="password" value=""/><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
  </body>
  </body>
</html>

struts.xml

<?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>
    <constant name="struts.devMode" value="true"></constant>  <!-- 是否是开发模式 -->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 指定字符集 -->
    <constant name="struts.locale" value="zh_CN"></constant> <!-- 指定国际化语言 -->
   
	<package name="ruge" extends="struts-default">
		<action name="login1" class="com.ruge.action.Login1">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>
login1.java

package com.ruge.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author 嘿丷如歌
 * @date 2017年7月1日 上午11:16:07
 * @description:
 *
 */
public class Login1  {
private String username;
	
	private String password;
	
	
	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 execute() throws Exception {
		if("admin".equals(username) && "admin".equals(password)){
			Map<String, Object> session = ActionContext.getContext().getSession();  
			session.put("userName",username); 
			return "success";
		}else{
			return "error";
		}
	}
	
	
}


success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    ${username},登录成功!
  </body>
</html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    登录失败!
  </body>
</html>

接下来是拦截器模块

先说一下拦截器有什么用

拦截器是对调用的Action起作用,它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
拦截器只能拦截Action,说明白点拦截器其实是Action的功能块,只在Action前后执行,初学者肯定会有疑问,把功能全都写在Action中就行了呀,为什么要把功能分出来,其实这个struts2的一个强大之处,你想想,假如这个功能块很多Action都要用,难道你的这些Action中都要写呀,就算复制粘贴也不方便呀,你把它做成功能块,哪个Action需要就在哪个Action中配置就好了,更好的方法是,创建一个公共的Action,把通用的东西全配置到这里面,其他Action引用(继承)就可以了
过滤器是拦截用户请求的,范围明显比拦截器大的多,你上网时肯定碰到过这中效果,你想下载个东西,点击下载先跳出登陆页面,这就是拦截器搞的鬼,没有登录前很多页面或Action都被他拦截了 
拦截器和过滤器的区别:过滤器是对整个的请求过程起作用!换句话说就是拦截器没有过滤器的范围广。







struts2默认的拦截器可以查看struts-default.xml

struts-default.xml文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器。
这些拦截器是以key-value对的形式配置在struts-default.xml中,其中name是拦截器名字,就是后面使用该拦截器的引用点,value
则指定拦截器的实现类。

拦截器的开发:在struts中 只有登录之后才能看到某个action的内容    登录失败是看不到数据的  怎么样  是不是这样一说就对拦截器功能熟悉了呢

此拦截器实现功能:用户需要指定用户名登陆,登陆成功进入相应页面执行操作,否则返回到登陆页面进行登陆,当直接访问操作页面(登陆后才能访问的页面)时则不允许,须返回登陆页面。


LoginInterceptor

package com.ruge.Interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @author 嘿丷如歌
 * @date 2017年7月1日 下午9:01:05
 * @description:
 *
 */
public class LoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		 // 取得请求相关的ActionContext实例
		ActionContext ac= arg0.getInvocationContext();
		
		Map session = ac.getSession();
		
		String user= (String) session.get("userName");
		
		if(user==null||user.equals("")){
			ac.put("tip", "请登录后再重新尝试");
		}else{
			 return arg0.invoke(); 
		}
		return "login";
	}

}

struts.xml

<?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>
    <constant name="struts.devMode" value="true"></constant>  <!-- 是否是开发模式 -->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 指定字符集 -->
    <constant name="struts.locale" value="zh_CN"></constant> <!-- 指定国际化语言 -->
   
	<package name="ruge" extends="struts-default">
	 <!-- 定义一个拦截器 -->  
	 <interceptors>
	 <interceptor name="authority" class="com.ruge.Interceptor.LoginInterceptor">  </interceptor>
	  <!-- 拦截器栈 -->  
            <interceptor-stack name="mydefault">  
               <interceptor-ref name="defaultStack"></interceptor-ref><!--默认拦截栈  -->
            <interceptor-ref name="authority"></interceptor-ref><!--自定义拦截栈  --> 
            </interceptor-stack>  
	 </interceptors>
	 
	 
		<action name="login1" class="com.ruge.action.Login1">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="login">/login.jsp</result>
		</action>
	</package>
</struts>

文件上传


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <a href="login.jsp">登录</a>
    <a href="fileUpload.jsp">单文件上传</a>
     <a href="fileDownload.jsp">单文件下载</a>
  </body>
</html>
struts.xml

<?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>
    <constant name="struts.devMode" value="true"></constant>  <!-- 是否是开发模式 -->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 指定字符集 -->
    <constant name="struts.locale" value="zh_CN"></constant> <!-- 指定国际化语言 -->
   
	<package name="ruge" extends="struts-default">
	 <!-- 定义一个拦截器 -->  
	 <interceptors>
	 <interceptor name="authority" class="com.ruge.Interceptor.LoginInterceptor">  </interceptor>
	  <!-- 拦截器栈 -->  
            <interceptor-stack name="mydefault">  
               <interceptor-ref name="defaultStack"></interceptor-ref><!--默认拦截栈  -->
            <interceptor-ref name="authority"></interceptor-ref><!--自定义拦截栈  --> 
            </interceptor-stack>  
	 </interceptors>
	 
	 
		<action name="login1" class="com.ruge.action.Login1">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="login">/login.jsp</result>
		</action>
		<action name="fileUpload" class="com.ruge.action.FileUploadAction">
		<result name="uploadsuccess">/uploadsuccess.jsp</result>
		</action>
		 <action name="fileDownload" class="com.ruge.action.FileDownloadAction">
		 <result name="success" type="stream">  
            </result>  
		</action> 
	</package>
</struts>

FileUploadAction

package com.ruge.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 嘿丷如歌
 * @date 2017年7月1日 下午10:27:28
 * @description:
 *
 */
public class FileUploadAction  extends ActionSupport{

	 private String username;
	    
        //注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件
	    private File file;
	    
	    //提交过来的file的名字
	    private String fileFileName;
	    
	    //提交过来的file的MIME类型
	    public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public File getFile() {
			return file;
		}

		public void setFile(File file) {
			this.file = file;
		}

		public String getFileFileName() {
			return fileFileName;
		}

		public void setFileFileName(String fileFileName) {
			this.fileFileName = fileFileName;
		}

		public String getFileContentType() {
			return fileContentType;
		}

		public void setFileContentType(String fileContentType) {
			this.fileContentType = fileContentType;
		}

		private String fileContentType;
	
		@Override
	    public String execute() throws Exception
	    {
			//获取文件存储路径
	        String root = ServletActionContext.getServletContext().getRealPath("/upload");
	        //输入流
	        InputStream is = new FileInputStream(file);
	        //输出流
	        OutputStream os = new FileOutputStream(new File(root, fileFileName));
	        
	        System.out.println("fileFileName: " + fileFileName);

           // 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同
	        System.out.println("fileName: " + file.getName());
	        System.out.println("filePath: " + file.getPath());
	        
	        byte[] buffer = new byte[500];
	        int length = 0;
	        
	        while(-1 != (length = is.read(buffer, 0, buffer.length)))
	        {
	            os.write(buffer);
	        }
	        
	        os.close();
	        is.close();
	        
	        return "uploadsuccess";
	    }
		
		
}

uploadsuccess.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>Insert title here</title>
</head>
<body>
    上传成功!
    <br/><br/>
    <!-- ${pageContext.request.contextPath} tomcat部署路径,
          如:D:\apache-tomcat-6.0.18\webapps\struts2_upload\ -->
          
       <img src="http://localhost:8080/webtest/upload/${fileFileName}"/>
       
       
</body>
</html>


文件下载

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <a href="login.jsp">登录</a>
    <a href="fileUpload.jsp">文件上传</a>
     <a href="fileDownload.jsp">文件下载</a>
  </body>
</html>

fileDownload.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>Insert title here</title>
</head>
<body>
<h2>文件下载内容:</h2><br/>  
<a href="fileDownload">图片下载</a>
<a href="http://localhost:8080/webtest/upload/note.txt">aaa</a>
</body>
</html>

使用fileDownload的方式我没成功  使用下面的直接访问文件路径的方式成功了   

如果哪位大大知道怎么使用fileDown的方式访问可以教教我,感谢

接下来贴出代码

FileDownloadAction

package com.ruge.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 嘿丷如歌
 * @date 2017年7月2日 上午10:37:10
 * @description:
 *
 */
public class FileDownloadAction extends ActionSupport {
	 private String basePath = ServletActionContext.getServletContext().getRealPath("/upload/note.txt");  
	    private String fileName;  
	    @Override  
	    public String execute() throws Exception {  
	    	 return SUCCESS;
	    }  
	    public InputStream getInputStream() throws FileNotFoundException{  
	        return new FileInputStream(new File(basePath, fileName));  
	    }  
	  
	    public String getFileName() throws UnsupportedEncodingException {  
	        return new String(fileName.getBytes(), "ISO-8859-1");  
	    }  
	  
	    public void setFileName(String fileName) {  
	        this.fileName = fileName;  
	    }  
      
   
}

good  struts暂时先写到这里 

接下来咱们搞一波hibernate

开搞之前怎么能少了hibernate tool的安装与使用呢      要是少了  累哭。。。


如歌在出博客的时候struts已经更新到2.5啦,后续还有更新欢迎读者说明 如歌会进行更新




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值