第一步:下载与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;
}
}
倘若完成以上代码未能实现你的功能,请留言