Struts2 注解配置

来源:互联网 发布:单片机之呼吸灯 led 编辑:程序博客网 时间:2024/05/16 10:05

struts.xml  放在src目录下

<?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.i18n.encoding" value="UTF-8"/>     <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开-->     <constant name="struts.action.extension" value="action,do"/>     <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->     <constant name="struts.configuration.xml.reload" value="true"/>     <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->     <constant name="struts.devMode" value="false"/>       <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->     <constant name="struts.serve.static.browserCache" value="false" />     <!-- 指定由spring负责action对象的创建        <constant name="struts.objectFactory" value="spring" />     -->     <!-- 是否开启动态方法调用-->     <constant name="struts.enable.DynamicMethodInvocation" value="false"/> </struts>

web.xml  放在 WEB-INF目录下

<?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>Struts2Test</display-name><!--  --> <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><!-- 配置欢迎页 --><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

action文件  放在com.action目录下

package com.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.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.convention.annotation.ParentPackage;import com.opensymphony.xwork2.ActionSupport;import com.service.PersonService;@Namespace("/person") //访问路径的包名@Results( { @Result(name = "success", location = "/index.jsp"),         @Result(name = "error", location = "/hello.jsp") }) @ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) public class PersonAction extends ActionSupport {public PersonAction() {// TODO Auto-generated constructor stub} @Action("login")  //访问路径的action名, 想要访问selUser 这个方法 地址为 http://localhost:8080/工程名/person/login public String selUser(){System.out.println("login-index"); return SUCCESS; }}

所需jar包

struts2-convention-plugin-2.3.16.jar


0 0