日期:2014-05-19  浏览次数:20597 次

J2EE学习笔记——Struts2的部署和使用

首先在 Struts2官网上下载 struts2的 包 网址 :http://struts.apache.org/download.cgi#struts221

用这个Full Distribution:

  • struts-2.3.8.-all.zip (76mb) [PGP] [MD5]

解压以后找到 lib里的jar 文件: 主要用 以下几个:



把这几个包 导入   你的web网站下的WEB-INF\lib中   ;      Struts部署完成:


下面开始使用    新建一个  webproject,   包含以下几项:



首先更改web.xml  文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- 定义struts2的核心Filter -->
  
  <filter>
  
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  
  
  </filter>
  
  <!-- 让Struts2的核心Filter拦截所有请求 -->
  
  
  
  <filter-mapping>
  
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  
  </filter-mapping>
  
  
  
  
  
</web-app>
-------------------------------------------------------------------------------------
Login.jsp页面:


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<%@taglib prefix="s" uri="/struts-tags"%>
<%
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>Login.jsp</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>
   <s:form action="login">
   
   <s:textfield name="username" key="用户名"></s:textfield>
   <s:textfield name="password" key="密码"></s:textfield>
   
   <s:submit key="login" value="登陆"></s:submit>
   
   
   </s:form>
  </body>
</html>


---------------------------------------------------------------------
error.jsp


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 h