`
plane
  • 浏览: 157491 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Struts2 注解配置 demo1

    博客分类:
  • java
阅读更多
from http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotation-example-1.html

这个例子当中需要注意的是:
1 必须引入struts2-convention-plugin-2.1.6包
2 WelcomeUser 类要放在 action命名的包下
3 welcome-user.jsp要放在web-inf/content目录下
4 具体请参见 Convention plug-in 运行原理



一搭建环境
jdk1.6 struts2.1.6 tomcat6.0
所需包
01.commons-fileupload-1.2.1
02.commons-io-1.3.2
03.commons-logging-1.1
04.freemarker-2.3.13
05.junit-3.8.1
06.ognl-2.6.11
07.spring-test-2.5.6
08.struts2-convention-plugin-2.1.6
09.struts2-core-2.1.6
10.xwork-2.1.2

二代码
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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Struts2_Annotations1</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


WelcomeUser.java
package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class WelcomeUser extends ActionSupport
{
    private String userName;

    private String message;
    
    public String execute()
    {
        message = "Welcome" + userName;
        return SUCCESS;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getMessage()
    {
        return message;
    }

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

}



index.jsp在web-inf下
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>Annotations1</title>
</head>
<body>
<body>
<s:form action="welcome-user">
	<s:textfield name="userName" label="User Name" />
	<s:submit />
</s:form>
</body>
</html>


welcome-user.jsp在web-inf/content目录下
<%@ 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>Welcome User</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>


三Convention plug-in 运行原理
the example works fine. Now lets see how the example works.

The Convention plug-in is the one which does everything in the background. The Convention plug-in does the following things.

    * By default the Convention plug-in looks for the action classes inside the following packages strut, struts2, action or actions. Here our package name is com.vaannila.action. Any package that matches these names will be considered as the root package for the Convention plug-in.
    * The action class should either implement com.opensymphony.xwork2.Action interface or the name of the action class should end with Action. Here we extend our WelcomeUser class from com.opensymphony.xwork2.ActionSupport which inturn implements com.opensymphony.xwork2.Action.
    * The Convention plug-in uses the action class name to map the action URL. Here our action class name is WelcomeUser and the URL is welcome-user. The plug-in converts the camel case class name to dashes to get the request URL.
    * Now the Convention plug-in knows which Action class to call for a particular request. The next step is to find which result to forward based on the return value of the execute method. By default the Convention plug-in will look for result pages in WEB-INF/content directory.
    * Now the Convention plug-in knows where to look for results, but it doesn't know which file to display inside the content directory. The Convention plug-in finds this with the help of the result code returned by the Action class. If "success" is returned then the Convention plug-in will look for a file name welcome-user-success.jsp or welcome-user.jsp . It need not be a jsp file it can be even a velocity or freemaker files. If the result value is "input" it will look for a file name welcome-user-input.jsp or welcome-user-input.vm or welcome-user-input.ftl.
分享到:
评论

相关推荐

    struts2demo全注解

    struts2将请求在Action中封装为Map并将配置文件放到web-info中还可以自定义配置文件位置就是不将struts.xml放到src下但还是不如spring mvc 的封装来得方便

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...

    struts2 demo

    struts2范例,含登录跳转,文件上传及不跳转上传,注解和配置2种方式,适合初学者和复习用,采用struts2.1

    SSH全注解整合demo

    整合使用最新版本的三大框架(即Struts2...参考文档:(感谢下边的两位原创作者,另外我在结合两个优点的基础之上,把所有的配置换成了注解的配置方案。) http://blog.csdn.net/songanling/article/details/22454973 ...

    Struts2.3.15 基于Annotation的小DEMO

    自己学Struts的注解,写的一个零配置小demo。 很简单很简单,只供新手学习。

    dwr配置文件和注解两种方式的两个demo,ssh项目使用dwr

    dwr配置文件和注解两种方式的两个demo, dwr框架在项目中的使用, 一个sprig3+dwr3+jpa+...一个sprig2+dwr2+hibernate3+struts1,基于配置文件的完整demo http://blog.csdn.net/zuxianghuang/article/details/7376247

    ssh2全注解demo(sql server 2008)

    基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)整合开发,真正实现零配置。记得修改数据源。

    Maven Struts-Spring-Hibernate全注解示例

    一、部分软件/库版本 Maven:3.0.5 struts2.2.3-spring3.0.6-hibernate3.6.6 ...2.全注解,实现零配置 3.对Service/Dao层做了封装: 实现了对任何通用CRUD(增删改查)操作,不用写任何Service/Dao层代码

    基于Struts2.18+Spring2.5+Hibernater3.3+Annotation注解开发的电子商务网站demo

    无聊之秋,做了个电子商务网站demo,巩固了一下对SSH三大开源框架的认识,比且融入了Annotation减少了xml配置文件的出现,本人只是个菜鸟,希望该资源能对一些朋友略有帮助!

    eclipse插件Demo

    首先我说下我做的这个插件:用于springMVC,由于springMVC没有像struts1 2 那样,有个专门的跳转路径的配置文件,导致springMVC不能直观方便的看到方法(Action)和路径对应的匹配,所以我这个插件会把eclipse里工作...

Global site tag (gtag.js) - Google Analytics