Struts2开发入门实例

新建一个web project项目,在此取名login,WebRoot下引入struts2的lib下的如下几个包:</span>


在web.xml文件中增加struts2的FilterDispatcher,修改后的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">  
    <!-- 过滤器 -->  
    <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>  
  
    <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  
</web-app>  

注意:struts2配置了一个过滤器来执行分发功能。

在index.jsp页面的body处替换如下标签内容:

<body>
   <a href="./test.action">my first struts2案例</a>  
  </body>

整个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="./test.action">my first struts2案例</a>  
  </body>
</html>

可以看到body处需要一个action来处理响应,因此,我们就创建一个action类。


其内容如下:

package cn.nm.action;

public class TestAction {
	public String  execute(){  
	       return "success";  
	   }  
}

然后我们需要在配置文件中进行相应配置来将两者联系起来,让我们在src下新建并编辑struts.xml文件


该文件内容如下:

<!DOCTYPE struts PUBLIC   
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
     "http://struts.apache.org/dtds/struts-2.0.dtd">  
<struts>  
<!-- packet是包必须继程struts-default -->  
<package name="testStruts" extends="struts-default" namespace="/">  
<!--action的名称必须与视图中的名称一致  class是致命处理的类 method致命在该类中若那个-->  
<action name="test" class="cn.nm.action.TestAction" method="execute">  
<!-- 跳转页面result 是结果 -->  
<result name="success" type="dispatcher">/sc.jsp</result>  
</action>  
</package>  
</struts>  

看到我们还需要一个结果页面,即配置文件中success应该跳转到的页面sc.jsp。

在WebRoot新建一个jsp页面,取名sc,内容如下:

<%@ 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%>">
    <meta http-equiv="Content-Type" content="text/html charset=gb2312">
    <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>
    <h1>struts2 success 成功!</h1>
  </body>
</html>

最后的工程树形结构如下:


在此编写完成,将其部署到tomcat,启动tomcat后在浏览器输入:http://localhost:8080/login/ 在浏览器页面可以看到如下页面:


点击页面中的标签,进入如下页面,注意url:


这样成功实现一个Struts工程开发。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值