struts2

8 篇文章 0 订阅

struts交由spring管理,项目实现效果为:输入url,进入登录界面,输入用户id和密码,后台根据id查库表,输出用户名,前台跳转至欢迎页面。

项目代码结构:
这里写图片描述

此项目就是在之前的springMVC项目springMVC基础上增加了struts2的实现,这里只重点描述一下action的实现

web.xml 增加struts2的配置

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

login.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
    <h1>Struts 2 Hello World Example</h1>

    <s:form action="Welcome">
        <s:textfield name="id" label="Id" />
        <s:password name="password" label="Password" />
        <s:submit />
    </s:form>

</body>
</html>

welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
    <h1>Struts 2 Hello World 示例</h1>

    <h2>
        Hello
        <s:property value="username" />
    </h2>

</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="default"  extends="struts-default">
        <action name="Login">
            <result>/login.jsp</result>
        </action>
        <action name="Welcome" class="com.htp.action.WelcomeAction">
            <result name="SUCCESS">/welcome_user.jsp</result>
        </action>
    </package>
</struts> 

spring-mvc.xml增加对action的管理

    <context:component-scan base-package="com.htp.action" />

action类的实现

package com.htp.action;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;

import org.objectweb.asm.commons.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.htp.bean.User;
import com.htp.serviceImpl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

//@Controller
@Component
@Scope("prototype")
public class WelcomeAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String id;
    private String password;

    @Autowired
    UserServiceImpl userServiceImpl;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    // all struts logic here
    public String execute() {

        System.out.println("execute......");
        System.out.println("id[" + id + "]");
        if (id == null || id == "") {
            System.out.println("id不可为空");
            return null;
        }

        List<User> rst = userServiceImpl.getUserById(Integer.parseInt(id));
        if (rst != null) {
            User user = rst.get(0);
            System.out.println("username[" + user.getName() + "]");
        }

        return "SUCCESS";

    }
}

该项目中,action类中使用了service实现类,最主要的是将struts2交由spring管理器,否则一直会报service为null,期间用到的struts相关的jar包如下:
这里写图片描述

其次,login.jsp 和 welcome.jsp务必放在webContent根目录下,否则运行时前台输入url一直提示404错,找不到页面。这一点上吃了很大的亏。

效果图如下:
这里写图片描述
这里写图片描述
这里写图片描述

其他注意点:
1、struts.xml必须放在src目录下
2、web.xml文件中的struts2的过滤器是否配置,看看 的配置,新版本的这项配置是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,若不是新版的,需要写成对应的版本中类名称

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值