使用struts2编写登录demo
工具:
eclipse
tomcat v8.0
struts-2.3.x
顺便附个下载:struts-2.3.29-all.zip
(上个月下载的2.3.28.1,因为S2-037,最新已经是2.3.29了)
1.新建web project
命名后点击next并勾选Generate web.xml
2.导入相关jar包
打开struts-2.3.28.1\apps目录,将struts2-blank.war解压缩,从解压得到的WEB-INF\lib目录获取jar包,添加到新建项目lib中。
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>LoginByStruts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.配置struts.xml文件
在src文件夹下面创建strutx.xml文件。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="user" extends="struts-default">
<action name="login" class="com.claude.action.Login" method="login">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
5.编写Bean
定义实体类Person
package com.claude.bean;
public class Person {
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;
}
}
6.编写Dao
模拟登录验证
package com.claude.dao;
import com.claude.bean.Person;
public class Dao {
public boolean isLogin(Person person) {
if (person.getUsername().equals(person.getPassword())) {
return true;
} else {
return false;
}
}
}
7.编写Action
LoginAction处理登录的业务逻辑
package com.claude.action;
import com.claude.bean.Person;
import com.claude.dao.Dao;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -1339567642215112514L;
private Person person;
public String login(){
Dao dao=new Dao();
boolean flag=dao.isLogin(person);
if(flag){
return SUCCESS;
}else{
return INPUT;
}
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
8.相关JSP
登录界面index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%-- <%@ taglib prefix="s" uri="/struts-tags"%> --%>
<html>
<head>
<title>Login</title>
<link href="bootstrap.min.css" rel="stylesheet">
<link href="signin.css" rel="stylesheet">
</head>
<body>
<%-- <s:form action="login">
<s:textfield name="person.username" label="username"></s:textfield>
<s:password name="person.password" label="password"></s:password>
<s:submit/>
</s:form> --%>
<h2>Login</h2>
<div class="container">
<form class="Login" action="login" method="post">
<label for="inputName" class="sr-only">Username</label> <input
type="text" id="inputName" class="form-control"
placeholder="UserName" name="person.username"> <br /> <label
for="inputPassword" class="sr-only">Password</label> <input
type="password" id="inputPassword" class="form-control"
placeholder="Password" name="person.password">
<div class="checkbox">
<label> <input type="checkbox" value="remember-me">
Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign
in</button>
</form>
</div>
</body>
</html>
登录成功success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>success page</title>
</head>
<body>
Success login. <br>
</body>
</html>
9.运行结果
登录页面
登录成功后
程序目录如下
此demo的struts2工作流程
(用Markdown做的,凑合看……)
遇到的一些问题和解决方法
(1)初次配置时,打开JSP出现404。再看控制台,打印出如下错误信息
16:53:04.209 [localhost-startStop-1] ERROR org.apache.struts2.dispatcher.Dispatcher - Dispatcher initialization failed
com.opensymphony.xwork2.config.ConfigurationException: Unable to load configuration.
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70) ~[xwork-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:970) ~[struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:438) ~[struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:482) [struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74) [struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57) [struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) [catalina.jar:8.0.33]
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) [catalina.jar:8.0.33]
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) [catalina.jar:8.0.33]
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4658) [catalina.jar:8.0.33]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5277) [catalina.jar:8.0.33]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:8.0.33]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [catalina.jar:8.0.33]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [catalina.jar:8.0.33]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_77]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_77]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
Caused by: com.opensymphony.xwork2.config.ConfigurationException: Cannot locate the chosen ObjectFactory implementation: spring
at org.apache.struts2.config.AbstractBeanSelectionProvider.alias(AbstractBeanSelectionProvider.java:74) ~[struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.config.AbstractBeanSelectionProvider.alias(AbstractBeanSelectionProvider.java:45) ~[struts2-core-2.3.28.1.jar:2.3.28.1]
at org.apache.struts2.config.DefaultBeanSelectionProvider.register(DefaultBeanSelectionProvider.java:363) ~[struts2-core-2.3.28.1.jar:2.3.28.1]
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:240) ~[xwork-core-2.3.28.1.jar:2.3.28.1]
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67) ~[xwork-core-2.3.28.1.jar:2.3.28.1]
... 17 more
不难发现居然在纯struts程序的中出现了spring的字眼,经过排查,发现错误原因是:struts.xml中多了一句
<constant name="struts.objectFactory" value="spring"></constant>
正常的配置绝对不会出现这种错误,怪就怪自己偷懒,从别处SSH项目里拷过来的配置文件,没改干净就用上了。
初学者一定要自己亲自配置,然后可以和现有项目对比,这样才容易发现细节问题。
(2)也是一个小问题,我在jsp页面中添加CSS时,发现没有起到作用。立即google了一下,按照很多人给出的方法,依次尝试(部分试过的代码已在index.jsp中加以注释),还是无果。不得已把CSS文件放在了程序根目录下。
(3)博客看了不少,从没自己写过。学的越多,越觉得记性不好使,笔记翻不过来,先写一篇非常简单的,算是给自己填补空白。代码过于简单,就不加注释了。研究了一下排版,尚不熟悉。如有看官路过,望多多包涵,不吝赐教。