Struts2基础-helloworld

来源:互联网 发布:社交软件 系统架构 编辑:程序博客网 时间:2024/06/10 18:59

Struts2开发步骤

1.新建web工程Strutsdemo并引入jar包:

commons-fileupload-1.2.1.jar

freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.jar

xwork-core-2.1.6.jar

2.在web.xml中添加控制器配置:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><filter><filter-name>strutsdemo</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>strutsdemo</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
3.创建action
    package demo;    public class Test {       private String name;public String execute(){name = "hello world";return "success";}public String getName() {return name;}}

4.在src下创建struts.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name = "demo1" extends = "struts-default"><action name = "welcome" class = "demo.Test"><result name = "success">hello.jsp</result></action></package></struts>

5.创建hello.jsp文件

<html>  <head>    <title>demo</title>  </head>  <body>   ${name } <br>  </body></html>

6.在浏览器地址栏发送请求:

http://127.0.0.1:8888/Strutsdemo/welcome.action

浏览器显示:

hello world