首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Struts2习题-基于注解方式Action配置

2012-09-23 
Struts2练习--基于注解方式Action配置还是已登录来说明下这个Action的配置,这里要说的Action的配置不是在s

Struts2练习--基于注解方式Action配置

还是已登录来说明下这个Action的配置,这里要说的Action的配置不是在src/struts.xml中,而是用注解方式来进行配置的

前提是除了基本的那六个jar包之外,还需要一个struts-2.1.8.1\lib\struts2-convention-plugin-2.1.8.1.jar

不过struts.xml还是要有的

具体示例

Login.jsp

?

<%@ 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>login</title>
</head>
<body>
<form action="test/hello-world.action">
??????? 姓名:<input type="text" name="username"/><br>
???????? 密码:<input type="password" name="password"/><br>
???????? <input type="submit" value="登陆 ">
??? </form>

</body>
</html>

?

?

?

?src/struts.xml

?

<!DOCTYPE struts PUBLIC
??? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
??? "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>?<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开? -->
?<constant name="struts.devMode" value="true" />
?<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->?
?<constant name="struts.action.extension" value="action,do"/>?
?<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开? -->?
?<constant name="struts.configuration.xml.reload" value="true"/>
?<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭? -->
?<constant name="struts.serve.static.browserCache" value="false" />??
?<constant name="struts.enable.DynamicMethodInvocation" value="false" />?
?<constant name="struts.ognl.allowStaticMethodAccess" value="true" />?
?<include file="struts-plt.xml" />
?<include file="struts-org_user.xml" /> -->
</struts>?<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">
?
?<!-- struts2.3.1 begin-->
?<filter>
??<filter-name>struts2</filter-name>
??<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
??<init-param>?
?????????? <param-name>actionPackages</param-name>?
?????????? <param-value>com.sspm.action</param-value>?
?????? </init-param>
?????? <init-param>
???? ??<param-name>config</param-name><!-- 服务器运行的时候加载struts 配置文件,没有注解似乎报错 -->
???? ??<param-value>struts-default.xml,struts-plugin.xml,WEB-INF/class/struts.xml</param-value>
??? ?</init-param>
?</filter>
?<filter-mapping>
??<filter-name>struts2</filter-name>
??<url-pattern>/*</url-pattern>
?</filter-mapping>
?<!-- struts2.3.1 end-->
?
? <welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>

?

HelloWorldAction.java

?

package com.sspm.action.test;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

/**
?* Struts2基于注解的Action配置
?* @author wangpj
?*
?*/
@ParentPackage("struts-default")
@Namespace("/test")
//@Namespace("")
@Results(
?? {
?? @Result(name="success",location="/jsp/ok.jsp"),
?? @Result(name="error",location="/jsp/errors.jsp")
?? }
)
@ExceptionMappings(
??{
???@ExceptionMapping(exception="java.lange.Exception",result="error")
??}
)
@InterceptorRefs({
?@InterceptorRef("defaultStack")
})
public class HelloWorldAction extends ActionSupport {

?private static final long serialVersionUID = 1L;
?
?private String username;
?private String password;
?
?//或者写成? @Action(value = "login")
?//请求url: http://localhost:8080/sshtest/test/login!login.action
?//请求其他方法: http://localhost:8080/sshtest/test/login!add.action
?// 跳转页 走的是全部Result
?@Action("login")
?public String login() throws Exception{
??System.out.println("===========");
???????? return SUCCESS;
?}
?
?//有“/” 命名空间被替换
?//请求url: http://localhost:8080/sshtest/other/add!add.action
?//请求其他方法???? http://localhost:8080/sshtest/other/add!login.action
?// 跳转页 走的是局部Result
?@Action(value="/other/add",results = { @Result(name = "success", location = "/index.jsp")})
?public String add() throws Exception{
??System.out.println("add>>>>>>");
??return SUCCESS;
?}

?//默认请求action类,去掉结尾"Action",然后按照驼峰规则添加“-”,字符全部小写
?//请求url: http://localhost:8080/sshtest/test/hello-world.action?username=abc&password=123
?@Override
?public String execute() throws Exception {
??//条件password.equals("123") 时 password为null时抛出异常 转向error
??if ("abc".equals(username)&&"123".equals(password)) {
???System.out.println("abc>>>>>>");
????? return SUCCESS;
???? } else {
????? System.out.println("other>>>>>>");
????? return ERROR;
???? }
?}

?public String getUsername() {
??return username;
?}

?public void setUsername(String username) {
??this.username = username;
?}

?public String getPassword() {
??return password;
?}

?public void setPassword(String password) {
??this.password = password;
?}

?
}

?

ok.jsp和errors.jsp我就不贴出来了


?

注解配置的解释

?

??1) @ParentPackage 指定父包

??2) @Namespace 指定命名空间

??3) @Results 一组结果的数组

??4) @Result(name="success",location="/msg.jsp") 一个结果的映射

??5) @Action(value="login") 指定某个请求处理方法的请求URL。注意,它不能添加在Action类上,要添加到方法上。

??6) @ExceptionMappings 一级声明异常的数组

??7) @ExceptionMapping 映射一个声明异常

? 8) @InterceptorRefs 拦截器

热点排行