Struts2 Hello World 注释

  前一篇是Struts2的是用xml文件配置而来。不过Struts2也可以使用Annotation。
下面看下注释的使用。
Struts2注解使用Struts2 convention plugin类支持的。常用Scanning Methodology 和
Naming Converter两种原理来实现


特别注意:@ResultPath默认是找EB-INF/content下的
因此如果要改为访问Root下就要加上Struts.xml
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.convention.result.path" value="/"/>
</struts>




1.Scanning Methodology
Scann工作原理有三点:
  1.扫描配置目录下面包名为"Struts, struts2, action or actions"
  2. 扫描匹配实现com.opensymphony.xwork2.action接口的类或者继承com.opensymphony.xwork2.ActionSupport类
  3.匹配文件名以Action结尾的文件(UserAction,LoginAction).



2.Nameing Converter
   Struts2 将对所有注解的文件名进行处理
比如 LoginAction.java
  1. 将LoginAction中的Action去掉
  2.将去掉后的Login变为小写login


[color=blue][size=medium]下面看用 Annotation例子
首先编写pom.xml
 <dependency>
          <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-core</artifactId>
	  <version>2.1.8</version>
    </dependency>
 
    <dependency>
          <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-convention-plugin</artifactId>
	  <version>2.1.8</version>
    </dependency>

2编写LoginAction,让LoginAction继承ActionSupport
  @Namespace("/User")
  @ResultPath(Value="/")
  @Result(name="success", location="pages/login.jsp")
 public Class LoginAction extends ActionSupport{

}


这个LoginAction.java就等同于struts xml配置的
   <package name="user" namespace="/User" extends="struts-default">
      <action name="Login">
      <result>pages/login.jsp</result>
   </action>
  </package>


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
 
import com.opensymphony.xwork2.ActionSupport;
 
@Namespace("/User")
@ResultPath(value="/")
public class WelcomeUserAction extends ActionSupport{
 
	private String username;
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	@Action(value="Welcome", results={
		@Result(name="success",location="pages/welcome_user.jsp")
	})
	public String execute() {
 
		return SUCCESS;
 
	}
}

这个WelcomeUserAction 类等同于下面的xml配置
<package name="user" namespace="/User" extends="struts-default">
   <action name="Welcome" class="com.david.user.action.WelcomeUserAction">
	<result name="SUCCESS">pages/welcome_user.jsp</result>
   </action>
</package>


jsp页面
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Hello World Annotation Example</h1>
 
<s:form action="Welcome">
	<s:textfield name="username" label="Username"/>
	<s:password name="password" label="Password"/>
	<s:submit/>
</s:form>
 
</body>
</html>

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Hello World Annotation Example</h1>
 
<h4>Hello <s:property value="username"/></h4>
 
</body>
</html>

记住这样用注解的是没有struts.xml这个文件的。
因此就配置下web.xml就可以了
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Struts 2 Web Application</display-name>
 
  <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>


最后http://localhost:8080/Struts2Example/User/login.action 运行下[/size][/color]

你可能感兴趣的:(Hello world)