您的位置:首页 > 编程语言 > Java开发

Struts2学习(一)

2016-07-25 13:49 381 查看

struts2 就是 web层开发框架,符合MVC模式

入门程序

 创建web工程

 

导入jar包

下载struts2的jar包  struts-2.3.15.1-all 版本.  

注意:在struts2开发,一般情况下最少导入的jar包,去apps下的struts2-blank示例程序中copy

  

创建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>
<h1>Hello Struts2</h1>
</body>
</html>

 创建hello.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>
<h1>Hello Struts2</h1>
</body>
</html>

 

对struts2框架进行配置

web.xml

 web.xml文件中配置前端控制器(核心控制器)-----就是一个Filter

 目的:是为了让struts2框架可以运行。

<?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" 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">
<display-name>struts2_01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>

 

struts2.xml

创建一个struts.xml配置文件 ,这个是struts2框架配置文件。

目的:是为了struts2框架流程可以执行。

名称:struts.xml

位置:src下(classes下)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="default" namespace="/" extends="struts-default">

<action name="hello" class="com.cxz.HelloAction" method="say">
<result name="good">/hello.jsp</result>
</action>
</package>

</struts>

 

创建一个HelloAction类

要求,在HelloAction类中创建一个返回值是String类型的方法,注意,无参数。

package com.cxz;

public class HelloAction {

public String say() {
System.out.println("Hello action say method");
return "good";
}

}

 

在struts.xml文件中配置HelloAction

<package name="default" namespace="/" extends="struts-default">

<action name="hello" class="com.cxz.HelloAction" method="say">
<result name="good">/hello.jsp</result>
</action>
</package>

 

在index.jsp中添加连接,测试

<a href="${pageContext.request.contextPath }/hello.action">访问Struts2入门</a>

运行

http://localhost:8080/struts2_01/

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: