struts2——第一個案例
阿新 • • 發佈:2018-08-04
spa struts-2 cat location mapping 例子 mls xtend 綠色
步驟如下
編程工具等百度雲分享
1、創建一個web項目
2、引入struts2的基本jar包
struts2的基本jar包百度雲
鏈接:https://pan.baidu.com/s/1LBnPJhFjqHuU7XW6m9xIqA 密碼:d6wg
3、新建一個web.xml
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"web.xml的代碼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
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>struts.xml的代碼
5、創建相應的Java類
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 }Hello類的代碼
6、創建相應的jsp文件
7、導入相應的servlet包
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>ok.jsp的代碼
tomcat官方下載連接——安裝版&綠色版
8、添加一個tomcat
9、啟動tomcat
10、輸入相應的地址訪問
struts2——第一個案例