第一次接触java
web开发,花费了一天半的时间,写了个简单的登录页面,以此文为记。
开发工具
Eclipse Luna Release (4.4.0) 已集成maven,maven目前的体会就是管理各种依赖
jetty为maven提供的一个插件,类似tomcat
struts2框架,MVC,action,jsp
mysql server 5.6
开发过程
1. 新建Maven web工程
Group Id: org.apache.maven.archetypes
Artifact Id: maven-archetype-webapp
2. 使用maven添加依赖及插件
在pom.xml的<dependencies></dependencies>间添加,或者是右键工程选择Maven->Add
Dependency
jsp页面支持
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> |
struts2
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3. |
mysql connector
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency> |
jetty
为插件,在<plugins></plugins>间添加,或者是右键工程选择Maven->Add
Plugin,接着在pom.xml中写配置
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.10</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <stopKey>foo</stopKey> <stopPort>9999</stopPort> </configuration> <executions> <execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <daemon>true</daemon> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> |
编译源码的插件
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> |
注意plugin的层次
<build> <plugins> <plugin>...</plugin> </plugins> </build> |
3. maven命令行
网上搜索资料,得知命令mvn jetty:run,可以启动jetty。
请教杨学长后,知道在Eclipse中,右键工程,选择run as中的run
configurations,打开界面如下
红框部分,即是maven命令输入的地方,如jetty:run
4. struts2框架开发登录页面
4.1 web.xml中添加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> |
4.2 struts.xml中添加action
<package name="struts2demo" namespace="/" extends="struts-default"> <action name="Login"> <result>login.jsp</result> </action> <action name="Welcome" class="struts2demo.WelcomeUserAction"> <result name="SUCCESS">welcome_user.jsp</result> <result name="ERROR">error.jsp</result> </action> </package> |
4.3 MVC
V->login.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
<s:form action="Welcome">
<s:textfield name="username" label="Username"/>
<s:password name="password" label="Password"/>
<s:submit/>
</s:form>
</body>
</html> |
M->WelcomeUserAction.java
package struts2demo;
import java.sql.*;
public class WelcomeUserAction {
//<s:textfield name="username">
you can get it from <s:property value="username">
private String username;
private String password;
//get and set method for struts2 fill in
//Username is the label name
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;
}
//"all struts logic here"
public String execute() {
//
try {
Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Success loading Mysql
Driver!");
}
catch (Exception e) {
//System.out.println("Error loading Mysql
Driver!");
e.printStackTrace();
}
//
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/zrss",
"root", "1234");
String sql = "select * from user where name
= ?";
PreparedStatement ps = connect.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
if (rs.getString("password").equals(password))
{
return "SUCCESS";
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return "ERROR";
}
} |
C->forward to SUCCESS(welcome_user.jsp) or ERROR(error.jsp) |
|