Struts2框架入门,第一个Struts2项目

3 篇文章 0 订阅

开始之前先上博主的项目结构图,方便理解:

楼主使用的是myeclipse进行的开发,静态资源是在WebRoot下(eclipse是在WebConent下,其他并无冲突,不影响开发)

首先:做框架项目首先需要jar包,导入jar包:如下是基本的jar包,放入lib文件夹下

第二:创建index.jsp页面

写好请求路径(博主本次写了三种访问方法,常用的是第二种,第一钟和第三种开发使用较少)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
	
  </head>
  
  <body>
    This is my JSP page.
    <h3>方法一:method方法</h3>
    <a href="${pageContext.request.contextPath}/add.action">增加 </a>
    <a href="${pageContext.request.contextPath}/delete.action">删除 </a>
    <a href="${pageContext.request.contextPath}/update.action">修改 </a>
    <a href="${pageContext.request.contextPath}/select.action">查询 </a>
    
    
    <h3>方法二:通配符方法 (product_后面的值会和xml中的method进行匹配)</h3>
    <a href="${pageContext.request.contextPath}/product_add1.action">增加商品 </a>
    <a href="${pageContext.request.contextPath}/product_delete1.action">删除商品 </a>
    <a href="${pageContext.request.contextPath}/product_update1.action">修改商品 </a>
    <a href="${pageContext.request.contextPath}/product_select1.action">查询商品 </a>
     <br>
     
     
     <h3>方法三:动态访问方法 (!是动态访问的关键符号)</h3>
    <a href="${pageContext.request.contextPath}/product!add1.action">增加商品 </a>
    <a href="${pageContext.request.contextPath}/product!delete1.action">删除商品 </a>
    <a href="${pageContext.request.contextPath}/product!update1.action">修改商品 </a>
    <a href="${pageContext.request.contextPath}/product!select1.action">查询商品 </a>
     <br>
     
     
     
  </body>
</html>

第三:创建java类

package cn.lekele.struts2.lqx;

import com.opensymphony.xwork2.ActionSupport;
//继承ActionSupport
public class Demo1Action extends ActionSupport{

	//方法名会与xml中的method进行匹配
	public String add1() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	public String delete1() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	public String update1() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	public String select1() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	
	
	
}

第四:配置sturts.xml文件(博主使用的是模块化的开发,在总的struts.xml中引入子的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>
 
 <!--   引入Demo1Action.java中所需要的struts.xml文件     -->

           <include file="cn/lekele/struts2/lqx/struts-demo1.xml"></include>

</struts>

具体的子的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>

       <!-- 打开动态访问配置 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
 <!-- 
              
              package标签:
              extends:固定写法,默认写struts-default
              namespace:名称全路径,与action中的name共同决定了请求路径
              action 中的name就是jsp的请求名 class就是.java文件的全路径
              result 中的name就是.java文件中return回来的值
               -->

         <package name="demo" extends="struts-default" namespace="/">
               <!-- 方法一:method方法 -->
               <!-- 配置总的result页面地址 统一跳转到MyJsp.jsp -->
           <global-results>
              <result name="success">MyJsp.jsp</result>
           </global-results>
           <action name="add" class="cn.lekele.struts2.lqx.Demo1Action" method="add1">
           </action>
           <action name="delete" class="cn.lekele.struts2.lqx.Demo1Action" method="delete1">
           </action>
           <action name="update" class="cn.lekele.struts2.lqx.Demo1Action" method="update1">
           </action>
           <action name="select" class="cn.lekele.struts2.lqx.Demo1Action" method="select1">
           </action>
           
           <!-- 方法二:通配符方法   *号自动匹配jsp页面中 product_后面的内容 -->
           <action name="product_*" class="cn.lekele.struts2.lqx.Demo1Action" method="{1}">
           </action>
          
          <!-- 方法三:动态访问的方法 -->
            <action name="product" class="cn.lekele.struts2.lqx.Demo1Action"></action> 
          
         </package>

</struts>

第五:最后一步:配置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>
  
  <!-- 配置拦截器 -->
  <filter>
  <filter-name>demo</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  <filter-name>demo</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值