SSH2搭建篇01——strusts2

本文详细介绍了一个集成SSH2(Struts2、Hibernate、Spring)项目的步骤,包括环境搭建、登录功能实现及自定义拦截器等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先说说集成SSH2最麻烦的是架包了,我strust2,hibernate用的是myeclipe8.5自带的架包,加这2个框架有个冲突是都有antlr-.jar 去掉低版本的就行,spring是3.1.0版本下载的包含源码点击打开链接,附上我的项目源码点击打开链接

(我就不贴出所有jar了,自己也有点搞糊涂了,因为jar少了多了调了很久,自己搞出来才有收获)


我是安装struts2-——hibernate——spring 来集成的

首先,搭建是trust2 ,这个可以在myeclipe8.5中右击--MyEclipe--Add strust 选择2.1 --finish ,也可以手动加上struts2的jar (MyEclipe加下把jar拿出来再自己手动加上-我是手动的)

1.web.xml   加上strust2 filter   这个是最新的filter  

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

 <!-- struts2 -->  
    <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>  

2.创建一个login.action  (简单的登录)

package com.yeshun.action;

import java.util.List;

import com.yeshun.bean.User;

public class LoginAction extends BaseAction {

	
	private static final long serialVersionUID = 1L;

	private String username;
	private String password;
	
	
	public String login(){
		System.out.println(username+" -  "+password);
		if("admin".equals(username) && "123456".equals(password)){
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			request.getSession().setAttribute("user", user);
			
			return SUCCESS;
		}else{
			request.setAttribute("msg", "用户名或密码错误!");
		}
		return LOGIN;
	}
	
	
	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;
	}


}


3.strust.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<!--
		<constant>标签主要是用来修改struts.properties配置文件信息,name和value分别相当于struts.properties文件中的name和value
	-->
	<constant name="struts.i18n.encoding" value="utf-8" />


	<package name="struts2" extends="struts-default">
		
		<action name="LoginAction" class="com.yeshun.action.LoginAction" method="login">
			<result name="success">/success.jsp</result>
			<result name="login">/login.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
		
		<action name="TestAction" class="com.yeshun.action.TestAction"
			method="test">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>    

3.login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
 	 <base href="<%=basePath%>">
    
    <title>登录页面</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>

 <center>
 <h1>登录</h1>
 <c:if test="${msg != null}"><font color="red">${msg}</font></c:if>
 <s:form action="LoginAction">
用户名:  <input type="text" name="username" size="20"> <br/>
密  码:  <input type="password" name="password" size="20">
  <s:submit value="提交" ></s:submit>
   </s:form>
</center>
  </body>
</html>

4.success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>  
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  </head>
  
  <body>
<font size="30" color="red">成功</font><br />
用户名:${user.username}
</html>

发布测试-成功就说明搭建成了!


二.struts2 主要是拦截器,那我们也配置一个登录的验证,上面如果登录成功会有一个session的当访问别的action是进行拦截,用MethodFilterInterceptor

要深入学习struts2的,我推荐个csdn的博客点击打开链接


1.strust.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<!--
		<constant>标签主要是用来修改struts.properties配置文件信息,name和value分别相当于struts.properties文件中的name和value
	-->
	<constant name="struts.i18n.encoding" value="utf-8" />

	<package name="default" extends="struts-default">
		<!-- 地址栏随便输入的时候,找不到对应的action,避免出现错误,配置默认的action -->
		<!-- 
		<default-action-ref name="defaultAction"></default-action-ref>
		<action name="defaultAction">
			<result>/index.jsp</result>
		</action>
		 -->
	</package>

	<!--struts2 自定义拦截器学习  -->
	<package name="struts2" extends="struts-default">

		<interceptors>
			<!-- 配置未登录进行操作的拦截器 -->
			<interceptor name="loginInterceptor" class="com.yeshun.intecepter.AuthorInterceptor">
				<param name="excludeMethods">login</param>
			</interceptor>
			<!-- 重新封装一个默认的拦截器栈 -->
			<interceptor-stack name="myDefaultStack">
				<interceptor-ref name="loginInterceptor" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<!-- 为这个包设置默认的拦截器栈 -->
		<default-interceptor-ref name="myDefaultStack" />
		<global-results>
			<result name="login">/login.jsp</result>
		</global-results>
		
		<action name="LoginAction" class="com.yeshun.action.LoginAction" method="login">
			<result name="success">/success.jsp</result>
			<result name="login">/login.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
		
		<action name="TestAction" class="com.yeshun.action.TestAction"
			method="test">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>    

2.AuthorInterceptor类

package com.yeshun.intecepter;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.yeshun.bean.User;

public class AuthorInterceptor extends MethodFilterInterceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		ActionContext context = invocation.getInvocationContext();

		// 通过ActionContext来获取httpRequest
		HttpServletRequest request = (HttpServletRequest) context
				.get(StrutsStatics.HTTP_REQUEST);
		// 也可以通过ServletActionContext来获取httpRequest
		// HttpServletRequest request = ServletActionContext.getRequest();
		// 取得根目录的绝对路径
		String currentURL = request.getRequestURI();
		// 截取到访问的相对路径,可以通过这个和权限表比较来进行相应的权限控制
		String targetURL = currentURL.substring(currentURL.indexOf("/", 1),
				currentURL.length());
		System.out.println(currentURL + ".............." + targetURL);

		// 通过ActionContext获取session的信息,以Map形式返回
		Map session = context.getSession();
		// 获取容器里面的username值,如果存在说明该用户已经登录,让他执行操作,如果未登录让他进行登录
		User user = (User) session.get("user");
		
		if (user != null) {
			invocation.invoke();
			System.out.println("username:"+user.getUsername());
		}
		request.setAttribute("msg", "对不起,你没有登录,不能进行该操作");
		return "login";

	}

}

然后,我们访问TestAction.ation  如果没有登录将跳到登录界面


struts2 配置好了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值