struts 简单入门实践-demo

1.下载所需jar包

官网地址:struts官网地址 



选择相应版本进行下载。


2.创建工程,引入jar包(eclipse)

   选择创建一个 Dynamic Web project 勾选默认生成web.xml文件,到struts-2.5.10-all\struts-2.5.10\lib 选择struts基础jar包:


log4j是需要的,否则启动会报错。

3.编写简单配置

3.1:配置web.xml文件(该文件是项目启动时加载的第一个文件)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>strutsDemo</display-name>
  
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts</filter-name>
   	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter 如果路径找不到,可通过eclipse快捷键ctrl+shift+t 输入StrutsPrepareAndExecuteFilter找到全名

配置拦截器的含义为:任何url请求都会分发给StrutsPrepareAndExecuteFilter来统一处理。

3.2 创建struts.xml  

在src目录下创建一个名为struts.xml的xml文件,内容(xsd约束配置可参考struts的文档或者struts-2.5.10-all\struts-2.5.10\apps里的样例书写)

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

	<package name="default" extends="struts-default">
		<action name="hello" class="com.BrightSt.action.Hello">
			<result>result.jsp</result><!--返回成功后跳转到result.jsp 下-->
		</action>
	
	</package>
    

</struts>
         3.2 编写java代码

package com.BrightSt.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 继承ActionSupport
 * @Title: Hello.java  
 * @Package com.BrightSt.action  
 * @Description:   
 * @author BrightSt    
 * @date 2017年2月19日 下午8:06:04  
 * @version V1.0
 */
public class Hello extends ActionSupport{

	/*
	 * 重写execute方法,默认会执行此方法
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		System.out.println("执行hello");
		return SUCCESS;
	}
	
}

3.3 创建成功的jsp页面(在项目的根目录下:WebContent)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 this is result.jsp
</body>
</html>


4. 发布项目并测试结果:


5.源码下载地址(百度网盘)csdn下载

(为什么csdn上传了资源一直不显示,csdn友们有遇到过这种情况吗,故通过网盘分享)


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值