第一部分 struts2 注解
1.网上搜了很多资料,使用的包各不相同,因此常会遇见各种NoClassDefFoundError
因此直接去官网下载最新的struts包2.3.4.1
http://struts.apache.org/download.cgi#struts2341
2.新建了Web工程将刚下载的包中所有jar加入你的lib
3.配置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</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>*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
4 新建一个action:LoginAction.java
package com.rabbit.demo.action; 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.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; /** * * @author hackpro * @date 2012-11-15 上午11:27:11 */ /* * 这个小Demo的主要作用就是温故一下Struts2 Action的注解 * 一般在一个项目实施开发中是不会配置struts.xml进行Action的转发或重定向的,其都是通过注解的方式来配置Action的 */ // /////////使用注解来配置Action/////////////////////////// @ParentPackage("struts-default") // 父包 @Namespace("") @Results({ @Result(name = com.opensymphony.xwork2.Action.SUCCESS, location = "/msg.jsp"), @Result(name = com.opensymphony.xwork2.Action.ERROR, location = "/erlogin.jsp") }) // @ExceptionMappings 一级声明异常的数组 // @ExceptionMapping 映射一个声明异常 @ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) public class LoginAction extends ActionSupport { private static final long serialVersionUID = -2554018432709689579L; private String loginName; private String pwd; // @Action(value="login") 指定某个请求处理方法的请求URL。注意,它不能添加在Action类上,要添加到方法上。 @Action(value = "loginName") public String login() throws Exception { if ("ztt".equalsIgnoreCase(loginName.trim()) && "lve".equalsIgnoreCase(pwd.trim())) { return SUCCESS; } else { System.out.println("==========="); return ERROR; } } @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") }) public String add() throws Exception { return SUCCESS; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
4. 看到上面的action,因此你知道需要建立多jsp
5. 启动项目,可能会报spring的相关包异常,逐步的删除你的jar;
asm-3.3.jar
asm-commons-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-convention-plugin-2.3.4.1.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
当然此方法很笨,但对于菜鸟级的我灰常实用
6.登录测试
7.需要完善配置的地方还很多,路漫漫期修远兮..
8.上源码