周末闲来记录点东西.
代码放在了oschina上:https://git.oschina.net/9902/gsm.git
1.使用maven创建java web项目
$ mvn -version Apache Maven 3.3.9 Maven home: /usr/share/maven Java version: 1.7.0_80, vendor: Oracle Corporation Java home: /opt/jdk1.7.0_80/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "linux", version: "4.6.3-040603-lowlatency", arch: "amd64", family: "unix"
$ mvn archetype:generate -DgroupId=com.gsm -DartifactId=gsm -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Interactive mode [INFO] Using property: groupId = com.gsm [INFO] Using property: artifactId = gsm Define value for property 'version': 1.0-SNAPSHOT: : 0.1 [INFO] Using property: package = com.gsm Confirm properties configuration: groupId: com.gsm artifactId: gsm version: 0.1 package: com.gsm Y: : Y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.gsm [INFO] Parameter: packageName, Value: com.gsm [INFO] Parameter: package, Value: com.gsm [INFO] Parameter: artifactId, Value: gsm [INFO] Parameter: basedir, Value: /workspace/oschina-code [INFO] Parameter: version, Value: 0.1 [INFO] project created from Old (1.x) Archetype in dir: /workspace/oschina-code/gsm [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:01 min [INFO] Finished at: 2016-07-10T12:20:39+08:00 [INFO] Final Memory: 16M/217M [INFO] ------------------------------------------------------------------------
2.生成idea项目
$ mvn idea:idea [INFO] Scanning for projects... Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.pom (7 KB at 2.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.jar (77 KB at 73.9 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-idea-plugin/maven-metadata.xml Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-idea-plugin/maven-metadata.xml (471 B at 0.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-idea-plugin/2.2.1/maven-idea-plugin-2.2.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-idea-plugin/2.2.1/maven-idea-plugin-2.2.1.pom (7 KB at 12.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-idea-plugin/2.2.1/maven-idea-plugin-2.2.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-idea-plugin/2.2.1/maven-idea-plugin-2.2.1.jar (53 KB at 38.9 KB/sec) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building gsm Maven Webapp 0.1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-idea-plugin:2.2.1:idea (default-cli) > generate-resources @ gsm >>> [INFO] [INFO] <<< maven-idea-plugin:2.2.1:idea (default-cli) < generate-resources @ gsm <<< [INFO] [INFO] --- maven-idea-plugin:2.2.1:idea (default-cli) @ gsm --- Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.1/maven-plugin-api-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.1/maven-plugin-api-2.0.1.pom (643 B at 1.2 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.1/maven-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.1/maven-2.0.1.pom (12 KB at 6.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0.1/maven-project-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0.1/maven-project-2.0.1.pom (2 KB at 3.2 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0.1/maven-profile-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0.1/maven-profile-2.0.1.pom (2 KB at 0.5 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.pom (3 KB at 1.9 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0.1/maven-artifact-manager-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0.1/maven-artifact-manager-2.0.1.pom (2 KB at 2.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0.1/maven-repository-metadata-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0.1/maven-repository-metadata-2.0.1.pom (2 KB at 0.5 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom (765 B at 1.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/wagon/wagon-provider-api/1.0-alpha-5/wagon-provider-api-1.0-alpha-5.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/wagon/wagon-provider-api/1.0-alpha-5/wagon-provider-api-1.0-alpha-5.pom (5 KB at 8.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.1/maven-plugin-api-2.0.1.jar Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0.1/maven-profile-2.0.1.jar Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.jar Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0.1/maven-project-2.0.1.jar Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0.1/maven-artifact-manager-2.0.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.1/maven-plugin-api-2.0.1.jar (9 KB at 15.5 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0.1/maven-repository-metadata-2.0.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0.1/maven-artifact-manager-2.0.1.jar (49 KB at 30.9 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0.1/maven-profile-2.0.1.jar (30 KB at 18.2 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/wagon/wagon-provider-api/1.0-alpha-5/wagon-provider-api-1.0-alpha-5.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0.1/maven-repository-metadata-2.0.1.jar (20 KB at 10.7 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/wagon/wagon-provider-api/1.0-alpha-5/wagon-provider-api-1.0-alpha-5.jar (51 KB at 21.0 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0.1/maven-project-2.0.1.jar (103 KB at 25.2 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.jar (79 KB at 16.1 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.jar (77 KB at 12.0 KB/sec) [INFO] jdkName is not set, using [java version1.7.0_80] as default. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 25.991 s [INFO] Finished at: 2016-07-10T12:21:23+08:00 [INFO] Final Memory: 13M/150M [INFO] ------------------------------------------------------------------------
3.加入springmvc及其他相关依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.12.Final</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
4.配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>
<display-name>Archetype Created Web Application</display-name><servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet><servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping></web-app>
5.配置springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.gsm.*" /> <!-- 支持spring3.0新的mvc注解 --> <mvc:annotation-driven /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
</beans>
6.mvn compile 一下
7.创建WEB-INF/jsp目录以及demo.jsp文件和UserController.java
demo.jsp:
<%-- Created by IntelliJ IDEA. User: lijs Date: 16-7-10 Time: 下午2:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${p.name} </body> </html>
UserController.java:
package com.gsm.controller;import com.gsm.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;/**
-
Created by lijs on 16-7-10.
*/
@Controller
@RequestMapping("/user")
public class UserController {@RequestMapping(value = "/getuser/{id}",method = RequestMethod.GET)
public ModelAndView getUser(@PathVariable("id") Integer id) {ModelAndView mv = new ModelAndView(); Person p = new Person(); p.setName("lijs"); p.setAddr("addr"); p.setAge(30); mv.addObject("p", p); mv.setViewName("hello"); return mv;
}
}
8.运行,浏览器访问:
竟然没有正确输出!!
9.解决EL表达式不输出问题
a.在JSP中通过JSP指令关掉EL忽略解析
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> ${p.name} </body> </html>
b.提高 Servlet 2.3为 Servlet 2.4或Servlet 2.5
<?xml version="1.0" encoding="UTF-8" ?><!--
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
-->
<web-app version="2.5">
<display-name>Archetype Created Web Application</display-name><servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet><servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping></web-app>
解决:
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于