期末的一个作业老师要求用注解完成,那么先简单的写一个基于注解的Action配置。
LoginAction部分代码:
@Action(value="loginUI",results={
@Result(name="loginUI",location="/index.jsp")
})
//跳转到登录页面
public String loginUI (){
return "loginUI";
}
@Action(value = "employee_login", results = { @Result(name="login", type = "redirect" ,
location="frist.action")})
//真正处理登录
public String login() throws Exception{
//System.out.println(model.getAccount()+model.getPassword());
Employee employee = employeeService.getById(model);
//System.out.println(employee);
// 获得response对象,项页面输出:
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
if(employee==null){
response.getWriter().println("<font color='red'>没有该员工号!</font>");
return "loginUI";
}
if (model.getPassword()==employee.getPassword()) {
// 登录失败
//addFieldError("employee", "用户名或密码不正确!");
response.getWriter().println("<font color='red'>密码不正确!</font>");
return "loginUI";
} else {
// 登录成功
ActionContext.getContext().getSession().put("employee", employee);
//ServletActionContext.getRequest().getSession().setAttribute("employee", employee);
return "login";
}
}
下面是login.jsp主要代码:
<form action="${pageContext.request.contextPath}/login.action"
method="post">
<input type="text" name="id" id="id" value="ACCOUNT"
οnfοcus="this.value = '';"
οnblur="if (this.value == '') {this.value = 'USERNAME';}">
<input
type="password" id="password" name="password" value="PASSWORD"
οnfοcus="this.value = '';"
οnblur="if (this.value == '') {this.value = 'PASSWORD';}">
<input
type="submit" name="submit" value="SIGN IN">
</form>
这样就完成啦!当然struts.xml不能有action标签要加上一些内容
<constant name="struts.devMode" value="false" />
<!-- struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化-->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.devMode" value="true" />
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
<constant name="struts.action.extension" value="action,do"/>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />