Struts2搭建

Struts环境搭建

背景:最近突然想温习一下Struts2,之前研究过,好久没有弄了。忘记的差不多了,在网上找了篇帖子,但是做下来,尽然跑不起来。很是无奈。研究了蛮久,终于跑成功了。一怒之下准备写篇wiki,用来方便有心人去学习。

不废话了先来个总图:

 


创建好项目以后,将struts2lib包引进去(放入WEB-INF/lib下面)。

lib包的下载地址:

http://struts.apache.org/download.cgi#struts2516


我选择的是这个,下载下来以后加压一下,在其lib下面可以找到,我把他们全部放到我的项目中了,事实上不需要那么多。

配置:
我们先写一个login.jsp页面。

现在我们要配置struts.xml ,web.xmlstruts.properties。其中struts.properties配置很重要,不配置的话导致页面访问不到。

struts.xml的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="login" extends="rest-default">
       <!--   <global-allowed-methods>index,show,create,update,destroy,deleteConfirm,edit,editNew</global-allowed-methods>
   		-->
   		<action name="login" class="strutsdemo.com.huawei.LoginAction" method="execute">
              <result name="success">/success.jsp</result>
              <result name="fail">/fail.jsp</result> 
          </action>
    </package>
     
</struts>

这里我们一定要注意版本。struts2.5和之前的还是有很大不一样的。


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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>strutsdemo</display-name>
  
  <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  
</web-app>

接着配置struts.properties
struts.mapper.indexMethodName=execute

struts.mapper.postMethodName=execute

配置这个的原因是运行环境的时候总是包错。(网上找的例子都没有这个配置)后来发现

在老版本struts中,action如果不写方法名称,或者请求不指定特定的方法,struts就会默认调用execute方法。但是在新版本中,rest架包的RestActionMapper指定了默认的方法是index。就会出现错误

get方法报错Wrong method was defined as an action method:index

post方法报错Wrong method was defined as an action method:create

这个错误可在struts.properties中添加两个配置项解决

struts.mapper.indexMethodName=execute

struts.mapper.postMethodName=execute

代码实现:
编写java
package strutsdemo.com.huawei;


import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {


    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
     public String execute()  
        {  
            if("liuyinhua".equals(this.username) && "33520".equals(this.password))  
                return "success";  
            else  
                return "fail";  
        }  
    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;
    }    
}

由于success.jsp和fail.jsp页面并不想让用户直接访问到,而是通过登录页面,做个简单的逻辑判断之后,在根据判断结果跳转到相应的页面。

success.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>Insert title here</title>
</head>
<body>

<body>
   ====>登录成功!<br />
    账号:${request.username}<br />
    密码:${request.password}
</body>

</body>
</html>
fail.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>Insert title here</title>
</head>
<body>

<body>
      ====>登录失败 !<br />
    账号:${request.username}<br />
    密码:${request.password}
</body>

</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值