`
123629996
  • 浏览: 291401 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

struts2入门

阅读更多

      与时俱进,随着时间的推移,不知道从什么时候开始,struts2已经开始慢慢普及起来,从事了软件开发的职业,就要不断的学习学习,再学习。不能跟不上步伐,跟不上就意味着淘汰。这几天没什么事做,学习下struts2,菜鸟级别的,做下记录,呵呵!

      先说一个笑话:说有一个程序员退休了,想练习书法,所以就买了文房四宝,开始学习,铺开纸张,想了半天写下了HELLO WORLD。笑话虽然是笑话,但说明了一个很有趣的现象,回想我们学习语言的经历,哪门语言不是从打印hello world开始的,所以这个框架的学习也从hello world开始。 

        1.首先下载struts2的包 ,下载位置在http://struts.apache.org;

        2.在myeclipse中搭建一个web程序,引入struts2的jar包,可以全部引入,但是有点多,实际上在开始学习的时候引入几个就可以了,位置在下载的struts2包里的apps/struts2-blank-2.2.1.war里面,用压缩软件打开将工程里的lib目录下的所有jar文件拷入到我们新建的工程就ok了,是不是很easy啊

        3.在第二步中所说的war包中找到struts.xml文件copy到本工程的src目录下,这样struts2开发的准备工作就做完了。

        4.配置web.xml文件,可以打开我们下载的struts2的包下的docs/ww/index.html帮助文件,找到web.xml文件的配置说明,将其内容拷贝到我们自己的配置文件中

 

<web-app id="WebApp_9" 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>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中配置了一个拦截器,而学过struts1的人都知道,struts1中配置的是servlet。需要说明的是在struts2.1.3以后的版本中都是将拦截器配制成上面的代码展示,之前的配置成如下代码

 

 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

 5.书写后台代码

package com.yjck.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String message;

	@Override
	public String execute() throws Exception {
		message = "Hello World";
		return super.execute();
	}

	public void setMessage(String message) {
		this.message = message;
	}
	public String getMessage(){
		return message;
	}

}

  6.配置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>
<package name="helloworld" extends="struts-default">
	<action name="helloworld" class="com.yjck.struts2.action.HelloWorld">
		<result>/helloworld.jsp</result>
	</action>
</package>
</struts>

  7书写helloworld.jsp页面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>helloworld</title>
  </head>  
  <body>
    <h2>这里是struts2的helloworld程序
    	<s:property value="message"/>
    </h2>
  </body>
</html>

  8至此,第一个struts2的helloworld的程序就完成了,部署工程后在浏览器输入http://localhost:8080/struts2_helloworld/helloworld即可看到效果,在URL中最后一个helloworld是在struts.xml文件中配置的action名字。

未完待续。。。。。。 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics