关于Struts2配置问题

来源:互联网 发布:手机电视互动软件 编辑:程序博客网 时间:2024/04/30 23:48

今天刚学struts框架,在配置struts时,出现了一些问题,通过一系列搜索后,解决了问题。为了避免以后遇到类似问题,所以在这里记录一下。

struts版本是struts-2.5.2

1.首先需要导入以下jar包到项目的/WEB-INF/lib下。

2.在/WEB-INF下创建web.xml,添加<filter>与<filter-mapping>

  <filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>
3.创建一个登陆案例演示

3.1创建login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!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><s:text name="loginPage"/></title></head><body><s:form action="login"><s:textfield name="username" key="user"/><s:textfield name="password" key="pass"/><s:submit key="login"/></s:form></body></html>
3.2创建LoginAction.java

package cn.javaweb.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private static final long serialVersionUID = 1L;private String username;private String password;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;}//定义处理用户请求的execute方法public String execute() throws Exception {if(getUsername().equals("admin") && getPassword().equals("123")) {ActionContext.getContext().getSession().put("user", getUsername());return SUCCESS;}return ERROR;}}
3.3创建success.jsp与error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!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><s:text name="SuccessPage"/></title></head><body><h1>欢迎<font color="red">${sessionScope.user }</font>登陆成功</h1></body></html>

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!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><s:text name="ErrorPage"/></title></head><body><h1>登陆失败,账号密码错误!</h1><a href="<s:url value='/loginForm.jsp'/>">重新登陆</a></body></html>
4.在src下创建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>    <constant name="struts.devMode" value="true"/>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />        <package name="default" extends="struts-default" namespace="/">        <action name="login" class="cn.javaweb.action.LoginAction">              <result name="success">/success.jsp</result>              <result name="error">/error.jsp</result>          </action>      </package></struts>

途中遇到的问题:

java.lang.ClassNotFoundException:检查自己web.xml的<filter-class>是否写对了类名和jar包是否正确导入

0 0