struts2基本配置(登录页面)

1、新建web项目TestStruts2。
2、添加5个核心依赖
commons-logging-1.0.4.jar;
freemarker-2.3.15.jar;
ognl-2.7.3.jar;
struts2-core-2.1.8.1.jar;
xwork-core-2.1.6.jar;
commons-fileupload-1.2.1.jar(非核心,但必须不引人启动报错);

3、src目录下增加struts2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.limf.action.LoginAction">
<result name="success">/main.jsp</result>
</action>
</package>
</struts>

4、web.xml增加filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

5、src下增加com.limf.action包,添加LoginAction.java

package com.limf.actin;

public class LoginAction
{
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;
}

public String execute() throws Exception
{
return "success";
}
}

6、WebRoot下增加login.jsp

<%@ page language="java"  pageEncoding="gbk"%>
<html>
  <body>
    <form action="login.action" method="post">
    <p>userName <input type="text" name="userName"/><p>
    <p>password <input type="password" name="password"/><p>
    <p><input type="submit" value="提交"/></p>
    </form>
  </body>
</html>

7、WebRoot下增加main.jsp(登录成功后转向的页面)

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<html>
<body>
<div>userName:<%=request.getParameter("userName") %></div>
<div>password:<%=request.getParameter("password") %></div>
</body>
</html>

8、输入http://localhost:8080/TestStruts2/login.jsp

你可能感兴趣的:(apache,jsp,xml,freemarker,struts)