struts2——第一个案例
步骤如下
编程工具等百度云分享
1、创建一个web项目
2、引入struts2的基本jar包
struts2的基本jar包百度云
链接:https://pan.baidu.com/s/1LBnPJhFjqHuU7XW6m9xIqA 密码:d6wg
3、新建一个web.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <filter> 8 <filter-name>struts2</filter-name> 9 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 10 </filter> 11 <filter-mapping> 12 <filter-name>struts2</filter-name> 13 <url-pattern>/*</url-pattern> 14 </filter-mapping> 15 <display-name></display-name> 16 <welcome-file-list> 17 <welcome-file>index.jsp</welcome-file> 18 </welcome-file-list> 19 </web-app>
4、创建一个struts.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="hello" extends="struts-default"> 7 <action name="hello" class="com.xiaostudy.web.Hello" method="print"> 8 <result name="ok" >/ok.jsp</result> 9 </action> 10 </package> 11 </struts>
5、创建相应的Java类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.xiaostudy.web; 2 3 public class Hello { 4 5 public String print() { 6 System.out.println("Hello类的print方法执行了。。。。"); 7 return "ok"; 8 } 9 10 }
6、创建相应的jsp文件
7、导入相应的servlet包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>struts2的一个例子</title> 8 </head> 9 <body> 10 okokokok 11 </body> 12 </html>
tomcat官方下载连接——安装版&绿色版
8、添加一个tomcat
9、启动tomcat
10、输入相应的地址访问
运行原理图