1. 程式人生 > >SSH框架學習------struts2(一)

SSH框架學習------struts2(一)

str struts2 oct 簡單 src package efi struts2配置 html

1.總的目錄

技術分享

2.所有程序

1)index.jsp很簡單

<%@ 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>
<a href="${pageContext.request.contextPath }/helloWorld.action">helloworld!</a>
</body>
</html

2)配web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http:java.sun.com/xmll/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置struts2核心控制器 -->
<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>
</web-app>

3)配struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定struts2配置文件的DTD -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<!-- Struts2的Action必須放在指定的包空間下定義 -->
<package name="hello" namespace="/" extends="struts-default">
<!-- 定義action,該action對應的類為cn.itcast.action.HelloWorldAction類 -->
<action name="helloWorld" class="cn.itcast.action.HelloWorldAction">
<!-- 定義處理結果和視圖資源之間的映射關系 -->
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

4)配Action的類,類名為:HelloWorldAction

package cn.itcast.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}

}

5)"success"成功後返回的jsp: success.jsp

<%@ 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>
成功!
</body>
</html>

SSH框架學習------struts2(一)