java strtus2 注解配置入门(一)

 因为工作的原因,所以接触到一些项目,有的项目虽然看着能有跟着做,可是具体里面的框架是别人配置的,具体框架还是不是非常的了解,所以这里在看一下我学到的

一点关于struts2中注解开发的一点点。

直接代码开始吧

第一.需要一个包(当然struts核心包不能少)

struts2-convention-plugin-2.3.15.3.jar 网上都可以下载到,版本应该都无所谓

第二.配置

我开始在网上找教程,很多都说0配置,其实不然。实际开发中,别人也不会真的连struts.xml都去掉的。这里只是入门的话,倒是可以去掉(加上去掉大家考虑,反正这里入门影响不大)

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>


<!-- 请求参数的编码方式--> <constant name="struts.i18n.encoding" value="UTF-8"/> <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开--> <constant name="struts.action.extension" value="action"/> <!-- 当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" /> <!-- 配置返回结果去哪里寻找 --> <constant name="struts.convention.result.path" value="/" /> <!-- 配置类去哪里寻找 --> <constant name="struts.convention.package.locators" value="action" />
</struts>

第三.web.xml

我开始在网上找教程,很多都说0配置,去掉struts.xml然后我以为web.xml里面的文件都直接不需要了,结果走了很多弯路

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  
  <filter>
      <filter-name>struts2</filter-name>
      <!-- 可在引用的jar包struts2-core-2.3.15.3.jar下的 org.apache.struts2.dispatcher.ng.filter找到-->
      <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>

第四:这里就开始到action了。

package com.almostman.action;

import org.apache.struts2.convention.annotation.Action;
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;

@ParentPackage("struts-default")
//或者写成@Action("loginx")
@Namespace("/Main") 
@Action(value="loginx",
        results=
            {
                @Result(name="success", location="/success.jsp"),
                @Result(name="input", location="/error.jsp")
            }
        )
public class LoginAction extends ActionSupport{

    private String username;
    private String password;
    
    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;
    }
    //访问路径:struts013/Main/loginx.action
    @Override
    public String execute() throws Exception {
        
        if("admin".equals(username) && "admin".equals(password)){
            return SUCCESS;
        }
        
        return INPUT;
    }
    //http://localhost:8081/struts013/Main/init.action?username=admin&password=admin
    @Action("init")    
    public String init() throws Exception {
        
        if("admin".equals(username) && "admin".equals(password)){
            return SUCCESS;
        }
        
        return INPUT;
    }
    @Action(value = "/Main/say", results = { @Result(name = "success", location = "/success.jsp") })    
    public String say() throws Exception {
        
        if("admin".equals(username) && "admin".equals(password)){
            return SUCCESS;
        }
        
        return INPUT;
    }

}

第五:jsp页面 login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'loginUI.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
  <form action="loginx.action">
    username:<input name="username" type="text"/><br>
    password:<input name="password" type="password" /><br>
    <input type="submit" value="Submit">
    </form>
  </body>
</html>

最后运行  127.0.0.1:8080/项目名/login.jsp

然后一debug,就可以看出问题。

源码:http://pan.baidu.com/s/1gfCX68r

原文地址:https://www.cnblogs.com/sunxun/p/5384724.html