Struts2(三)通配符—Java基礎篇

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"
version="3.1">
<display-name>StrutsDemo</display-name>


<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.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>

2.struts.xml

<?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>


<!--bean 標籤用於創建一個JavaBean實例 -->
<!--constant 標籤用於默認行爲 -->
<!--package 包標籤用於區分不同的請求,比如網站前臺請求、網站後臺請求 -->


<!--配置web默認編碼集,相當於HttpServletRequest.setChartacterEncoding用法 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--默認我們struts2的請求後綴是.action,也就是說我們不配置該元素,action/do都可以 -->
<constant name="struts.action.extension" value="action,do"></constant>
<!--設置瀏覽器是否緩存靜態內容,默認值是true,在我們開發階段建議關閉,防止修改後測試失敗 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!--當struts配置文件修改後,系統是否自動重新加載該文件,默認爲false -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--開發模式下使用,這樣可以打印出更加詳細的錯誤信息 -->
<constant name="struts.devMode" value="true"></constant>
<!--默認視圖主題 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 是否開啓動態方法調用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />


<!--name包名,用於被別的包調用或繼承 namespace="" -->
<package name="uweb" extends="struts-default">
<!--action相當於以前的servlet的概念,對應一個請求name爲請求地址的url地址 localhost:8080/項目名/user/login.do -->
<!-- <action name="login" class="com.jike.action.LoginAction"> <result 
name="success">/index.jsp</result> <result name="error">/login.jsp</result> 
</action> -->


<action name="*_*" class="com.jike.action.{1}" method="{2}">
<result name="success">/success.jsp</result>
<allowed-methods>add,show,update,delete</allowed-methods>
</action>
</package>

</struts>

3.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
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" "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>
<h3>This is index.jsp</h3>
<h3>用戶</h3>
<a href="<%=path%>/UserAction_add.action">add</a>
<br>
<a href="<%=path%>/UserAction_show.action">show</a>
<br>

<h3>新聞</h3>
<a href="<%=path%>/NewsAction_add.action">add</a>
<br>
<a href="<%=path%>/NewsAction_show.action">show</a>
<br>
</body>

</html>

4.UserAction.java

package com.jike.action;


import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport {


private static final long serialVersionUID = 2558343769361074535L;


public String add() throws Exception {


System.out.println("User Add");
return SUCCESS;
}


public String show() throws Exception {


System.out.println("User Show");
return SUCCESS;
}
}

5.NewsAction.java

package com.jike.action;


import com.opensymphony.xwork2.ActionSupport;


public class NewsAction extends ActionSupport {
/**

*/
private static final long serialVersionUID = 1L;


public String add() throws Exception{
System.out.println("News Add");
return SUCCESS;
}


public String show() throws Exception {


System.out.println("News Show");
return SUCCESS;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章