strust2基于注解的action配置步骤

本文详细介绍Struts2框架的配置步骤,包括依赖文件下载、Struts.xml与web.xml配置、JSP页面与控制器编写等关键环节,并提供了一个登录控制器的具体实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:下载与struts2-core-2.3.34.jar版本相同的struts2-convention-plugin-2.3.34.jar放入lib文件下

第二步:配置Strust.xml文件

<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8" />

<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->

<constant name="struts.action.extension" value="action,do,htm" />

<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />

<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->

<constant name="struts.configuration.xml.reload" value="true" />

<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->

<constant name="struts.devMode" value="true" />

<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->

<constant name="struts.serve.static.browserCache" value="false" />

<!-- 是否开启动态方法调用 -->

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

第三步:配置web.xml


将此处的配置与Strust.xml中的<constant name="struts.action.extension" value="action,do,htm" />设置一直

第四步:书写jsp,并设置jsp中的action


第五步:书写controller代码并配置相关注解(注意:controller的包名必须以.action结束,例:com.lms.annotation.action)

package com.lms.annotation.action;
import java.util.List;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.lms.annotation.pojo.UserInfo;
import com.lms.annotation.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 登录控制器
 * 
 * @author hzh
 *
 */
@Controller("loginAction")
@Scope("prototype") // 设置action为多例模式
@ParentPackage("struts-default")
@Namespace("/jsp/login")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class loginAction extends ActionSupport {
@Autowired
LoginService loginService;
private static final long serialVersionUID = 1L;
private String identity;
private String password;
/**
* 登录

* @return
*/
@Action(value = "login", results = {
@Result(name = "success", location = "/jsp/index/index.jsp"),
@Result(name = "error", location = "/jsp/index/error.jsp") })
public String login() {
String jumpPage = null;
List<UserInfo> userInfos = loginService.getUserInfo(identity, password);
if (!userInfos.isEmpty()) {
jumpPage = SUCCESS;
} else {
jumpPage = ERROR;
}
return jumpPage;
}
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static long getSerialversionuid() {
return serialVersionUID;
}

}

倘若完成以上代码未能实现你的功能,请留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值