struts2 零配置
一、新建一個web項目,命名為:struts2
?
二、導入strut2所需的jar包 所需jar下載:http://pan.baidu.com/s/1dDxP4Z3
三、配置struts2的啟動文件,在web.xml添加如下內容
<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>四、在src包下,新建struts.xml和struts.properties這2個文件
在struts.xml添加如下內容
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 請求參數的編碼方式 --> <constant name="struts.i18n.encoding" value="UTF-8"/> <!-- 當struts.xml改動后,是否重新加載。默認值為false(生產環境下使用),開發階段最好打開 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 是否使用struts的開發模式。開發模式會有更多的調試信息。默認值為false(生產環境下使用),開發階段最好打開 --> <constant name="struts.devMode" value="true"/> <!-- 設置瀏覽器是否緩存靜態內容。默認值為true(生產環境下使用),開發階段最好關閉 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 含有Action類的路 從action包開始--><constant name="struts.convention.package.locators" value="action" /><package name="json" extends="json-default"></package></struts>在struts.properties文件添加如下內容(指定結果頁面的路徑)
struts.convention.result.path=/五、現在我們已經完成了strut2的環境配置了,接下來說介紹個使用的demo
demo1
在src下新建一個包命名為:com,在com包下新建一個包命名為:action,在action包下新建一個包命名為:demo
在demo 包下新建一個IndexAction.java的類
package com.action.demo;import org.apache.struts2.convention.annotation.ParentPackage; import com.opensymphony.xwork2.ActionSupport;@ParentPackage(value = "struts-default") public class IndexAction extends ActionSupport {/*** */private static final long serialVersionUID = -903752277625921821L; private String name; @Override public String execute() { setName("stuts2 零配置的實現"); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }在WebRoot目錄下新建一個文件夾命名為:demo
在WebRoot/demo目錄新建一個index.jsp 內容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <% 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><s:property value="name"/> </body> </html>啟動tomcat,在瀏覽器輸入:http://localhost:8080/struts2/demo/index
?
demo2
在src/com/action/demo包下新建一個類IndexTestAction.java
package com.action.demo;import org.apache.struts2.convention.annotation.Action; import com.opensymphony.xwork2.ActionSupport;@Action public class IndexTestAction extends ActionSupport {/*** */private static final long serialVersionUID = -903752277625921821L;private String name; @Override public String execute() { setName("stuts2 零配置的實現,路徑配置," + "IndexTestAction對應的結果界面:WebRoot/demo/index-text.jsp action去掉," + "中間有大寫的轉換成小寫,加上'-' " + "例如:TestDemoAction 結果頁面的是:test-demo.jsp"); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }在WebRoot/demo目錄新建一個index-test.jsp 內容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <% 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><s:property value="name"/> </body> </html>?
啟動tomcat,在瀏覽器輸入:http://localhost:8080/struts2/demo/index-test
轉載于:https://www.cnblogs.com/wuweidu/p/3841297.html
總結
以上是生活随笔為你收集整理的struts2 零配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hdu3787
- 下一篇: selenium使用js进行点击