struts2全注解配置步骤

第一步:

创建“struts2annotation”的web项目

第二步:

导入需要的jar包(我的环境tomcat7加jdk7)

第三步:

修改web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">

    <display-name>Struts Blank</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.html</welcome-file>
    </welcome-file-list>

</web-app>




第四步:
配置struts.xml:


<?xml version="1.0" encoding="UTF-8"?>
<!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,do,go,qqi"/>  
    当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>  -->
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"/>  
	<constant name="struts.action.extension" value="action,do,go,qqi"/>
</struts>

第五步:创建action类:


package com.ninemax.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;

/**
 * 使用注解来配置Action
 *
 */
@ParentPackage(value = "struts-default")
@Namespace(value = "/user")
@Results({@Result(name = "success", location="/msg.jsp"),
		@Result(name="error",location="/error.jsp")})
@ExceptionMappings({@ExceptionMapping(exception="java.lange.RuntimeException",result="error")})

public class LoginAction extends ActionSupport {
	private static final long serialVersionUID = -2315464632166461L;
	private String loginname;
	private String pwd;
	
	@Action(value = "login")
	public String login() throws Exception{
		
		if("qq".equals(loginname) && "123".equals(pwd)){
			return "success";
		} else {
			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;
	}
	
}

第六步:
创建对应的jsp页面
1》、创建index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <title>Struts2登录验证</title>  
    <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>  
  <h3>Struts2登录</h3><hr/>  
    <form action="${pageContext.request.contextPath}/user/login.action" method="post">  
        <table border="1" width="500px">  
            <tr>  
                <td>用户名</td>  
                <td><input type="text" name="loginname"/></td>  
            </tr>  
            <tr>  
                <td>密码</td>  
                <td><input type="password" name="pwd"/></td>  
            </tr>  
            <tr>  
                <td colspan="2"><input type="submit" value="登录"/></td>  
            </tr>  
        </table>  
    </form>  
  </body>  
</html>



2》、创建msg.jsp页面


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <title>Struts2登录验证</title>  
    <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>  
  <h3>Struts2登录</h3><hr/>  
    登陆成功了~~
  </body>  
</html>

3》、创建error.jsp页面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'error.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>
    This is error JSP page. <br>
  </body>
</html>

第七步:
把项目部到tomcat下启动,并且访问~!
访问的地址拼接为:http://localhost:8080/+项目名称+namespace+action名
例如我的为:http://localhost:8080/struts2annotation/user/add.action


注意在创建action类时所在的包路径名必须包含action,否则会报错。。。(其实也可以是其它几个关键字)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值