Struts2学习笔记--安装struts2

本文详细介绍如何搭建Struts2框架开发环境,包括所需依赖包配置、Tomcat服务器设置、Struts2安装步骤、Action类编写及配置过程,并提供示例代码说明。

本历程开发环境

 服务器: Tomcat6.0

 I D E : MyEclipse


1. 将用到的包

    commons-fileupload-1.3.jar

    commons-io-2.2.jar

    commons-lang3-3.1.jar

    commons-logging-1.1.3.jar

    freemarker-2.3.19.jar

    javassist-3.11.0.GA.jar(这个包可以在app里面的历程拷贝出来)

    ognl-3.0.6.jar

    struts2-core-2.3.16.jar

    xwork-core-2.3.16.jar

拷贝到web-inf下的lib

2.配置tomcat

在server.xml中</host>前一行添加如下代码:
     <Context path="/struts2" docBase="E:\java1\Struts2\WebRoot" reloadable="true" />


3.安装struts2

因为struts2的入口点是一个Filter,所以需要在web.xml下按Filter的方式配置struts2

web.xml代码如下

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="3.0" 
  	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_3_0.xsd">
    <display-name></display-name>
    	
    <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>
    
  </web-app>

4. 编写Action类

Action类是Struts2处理请求的类,struts2必需的,需要继承com.opensymphony.xwork2.ActionSupport类,下面是处理注册信息的Aciotn

LoginAction.java

package struts;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport {

 

private String username;

private String password;

 

public String execute() throws Exception {

System.out.println("execute invoke!");

 

return "success";

 

}

 

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;

}

}


从上面代码来看,Action类就一个特点,就是要覆盖execute()方法,返回一个String处理结果

5. 配置Action

配置Action类就需要用到struts.xml, 但是MyEclipse中默认视图是没有显示web-inf目录下的class文件夹的struts.xml恰好位于class文件夹内,所以这里一个小技巧就是在src文件夹中新建一个struts.xml配置文件, MyEclipse会自动将struts.xml同步到class文件夹下

下面是struts.xml的代码:

  <?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>
  	<package name="struts" extends="struts-default">
  	
  		<action name="login" class="struts.LoginAction">
  		
  			<result name="success">result.jsp</result>
  			
  		</action>
  	
  	</package>
  
  </struts>

具体配置信息可以查看struts-2.3.dtd

在<struts>标签中可以有多个<package>,第一个<package>可以指定一个Servlet访问路径

(不包括动作名),如“/mystruts”。extends属性继承一个默认的配置文件“struts-default”,一般都继承于它,大家可以先不去管它。<action>标签中的name属性表示动作名,class表示动作类名。

<result>标签的name实际上就是execute方法返回的字符串,如果返回的是“success”,

就跳转到result.jsp. 页面在<struts>中可以有多个<package>,在<package>中可以有多个<action>。我们可以用如下的URL来访问这个动作:

http://localhost:8080/struts2/login.action


6. 编写用户交互接口

1) 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>Login 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>
     
     <form action="login">
     		username: <input type="text" name="username"/> <br>
     		password: <input type="password" name="password" /> <br>
     		<input type="submit" value="提交" />
     </form>
     
    </body>
  </html>

表单中的Action就是struts.xmlActionname,其他的和传统的jsp一样

2)result.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>Result 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: ${requestScope.username }<br>
      password: ${requestScope.password }
    </body>
  </html>

也可以使用request来获取username和password

最后启动tomcat, 在浏览器中输入如下URL:

http://localhost:8080/struts2/login.jsp 来进行访问


本人是新手自学菜鸟,如有什么错误的可以在评论留言,我会及时的更正自己的错误


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值