第一个Struts2入门经典实例
2014-02-21 11:21
330 查看
搭建好开发环境,现在开始进入第一个Struts入门项目。
我用的是MyEclipse工具,首先新建一个动态Web项目HelloWorld,如图
在MyEclipse中配置Tomcat
从MyEclipse菜单栏window中进入preferences,在搜索框搜索“tomcat",下面出现几个tomcat,将不需要的tomcat禁用掉(Disable),只让一个你已配置好的tomcat运行(Enable),然后在tomcat home directory 设置你安装的tomcat的目录。
在MyEclipse中启动tomcat,下面的控制台会出现Server startup的提示
到官网http://struts.apache.org/download.cgi下载最新的structs2,我们需要将下载好的structs2解压,然后将其中的这些jar包复制到 WEB_INF\lib 这个文件夹中
注:(commons-lang-x.y.jar这个包需要更换为新版的commons-lang3-x.y.jar包)
创建Aciton类 HelloworldAction.java
创建视图(前端jsp界面)HelloWorld.jsp
创建主页面 index.jsp
配置struts.xml
配置web.xml
将项目export为war包放到tomcat安装目录下的webapps文件下,此时的服务器是启动着的,会自动部属新的war包。在MyEclipse上run这个项目,会在它的浏览器上弹出index.jsp界面,如图
输入名字zlb,点击say hello
第一个struts2入门实例就此结束了。
我用的是MyEclipse工具,首先新建一个动态Web项目HelloWorld,如图
在MyEclipse中配置Tomcat
从MyEclipse菜单栏window中进入preferences,在搜索框搜索“tomcat",下面出现几个tomcat,将不需要的tomcat禁用掉(Disable),只让一个你已配置好的tomcat运行(Enable),然后在tomcat home directory 设置你安装的tomcat的目录。
在MyEclipse中启动tomcat,下面的控制台会出现Server startup的提示
到官网http://struts.apache.org/download.cgi下载最新的structs2,我们需要将下载好的structs2解压,然后将其中的这些jar包复制到 WEB_INF\lib 这个文件夹中
注:(commons-lang-x.y.jar这个包需要更换为新版的commons-lang3-x.y.jar包)
创建Aciton类 HelloworldAction.java
package com.myjava.zlb.HelloworldAction; public class HelloworldAction { private String name; public String execute()throws Exception{ return "success"; } public String getName(){ return name; } public void setName(){ this.name = name; } }
创建视图(前端jsp界面)HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> Hello World, <s:property value="name"/> </body> </html>
创建主页面 index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.myjava.zlb.HelloworldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
配置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/xml/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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
将项目export为war包放到tomcat安装目录下的webapps文件下,此时的服务器是启动着的,会自动部属新的war包。在MyEclipse上run这个项目,会在它的浏览器上弹出index.jsp界面,如图
输入名字zlb,点击say hello
第一个struts2入门实例就此结束了。
相关文章推荐
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- xcode4.3 经典入门 开发第一个IOS应用实例【转】
- Struts2入门经典实例
- Struts2入门经典实例
- Struts2入门经典实例-登录页面
- python 网络爬虫入门(1)———第一个python爬虫实例
- ios开发入门级实例(一步步的很详细,我就是这样学习的第一个案例)
- Struts2 第一个入门案例
- scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld
- C++多线程编程入门之经典实例
- MyBatis经典入门实例
- C++多线程编程入门之经典实例
- C++多线程编程入门之经典实例
- struts2入门-第一个例子
- Java Struts2 框架入门详解(三)简单的实例
- struts2 helloworld入门实例
- scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld
- struts2入门实例
- lucene3.6.1 经典案例 入门实例
- 学习struts2的第一个小实例