STRUTS2框架第一课-HELLOSTRUTS2
创建步骤
1.Eclipse-new-Dynamic Web Project
- 若没有选择2.5版本的web module,则需要操作: 右键工程名- java EE tools - Genarate Deployment Descriptor Stub;
3.引入jar包到 WebContent下的lib中
- 编写执行类HelloStruts2和方法sayHello()
package com.chyod.action;
public class HelloStruts2 {
public String sayHello() {
return "helloStruts2";
}
}
5.创建struts.xml 注意要放在src目录下,名字不能错,不能改
编辑dtd规则
<?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>
<package name="" namespace="/" extends="struts-default" >
<action name="hello" class="com.chyod.action.HelloStruts2" method="sayHello">
<result name="helloStruts2">/HelloStruts2.jsp</result>
</action>
</package>
</struts>
- 编写简易的响应jsp文件–HelloStruts2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Struts2</title>
</head>
<body>
<h1>Hello Struts2</h1>
<h2>This is my first demo of Struts2</h2>
<h3>The End!</h3>
</body>
</html>
- 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>hello_struts2</display-name>
<filter>
<filter-name>myfilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
8.运行结果: 注意我是用tomcat配置的本地服务器, 端口号为8888 工程名: hello_struts2 过滤的动作为: *.Action
如有不足,请多指教!
谢谢!