springMvc环境搭建
spring mvc 搭建的环境:
搭建一个maven工程(编辑环境是eclipse)
出现问题时解决:https://www.cnblogs.com/goldenVip/p/6278377.html
在maven工程集成spring
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>springMvcTest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springMvcTest Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <springframework-version>4.2.5.RELEASE</springframework-version> <spring-session-version>1.1.0.RELEASE</spring-session-version> <logback-version>1.2.3</logback-version> </properties> <dependencies> <!-- 集成spring mvc*************************start --> <!-- spring-test 对spring框架进行单元测试 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springframework-version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>${springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>${springframework-version}</version> </dependency> <!-- 集成spring mvc*************************end --> <!-- 单元测试依赖包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 写日志依赖包 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> </dependencies> <build> <finalName>springMvcTest</finalName> <!-- 解决每次使用Maven > Update project的时候JDK版本都恢复成1.5。的问题 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
web.xml 中配置servlet
在默认情况下,如例子中一样, servlet-name - servlet.xml 默认放在/WEB-INF文件夹下
可以自己定义servlet文件的位置
<servlet> <servlet-name>helloWeb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>helloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
其中spring-mvc.xml为servlet的配置文件,在src/main/resources文件路径下。
spring-mvc.xml 文件中配置servlet的具体信息
<!-- 配置需要扫描的包 --> <context:component-scan base-package="com.test.controller" /> <!-- 配置视图解析器 --> <!--视图文件的 前缀为/WEB-INF/ 后缀为.jsp的文件 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean>
前端jsp页面中用form表单提交
<form action="${pageContext.request.contextPath}/student/save" method="post"> <table> <tr><td>name</td><td><input type="text" name="name"></input></td></tr> <tr><td>age</td><td><input type="text" name="age"></input></td></tr> <tr><td colspan="2"><input type="submit" value="submit"></input></td></tr> </table> </form>
后端java 用一个实体类去接收
@RequestMapping(value="/save",method=RequestMethod.POST) public ModelAndView save(@ModelAttribute Student student){ ModelAndView view =new ModelAndView("result"); return view; }
参考文章:w3cSchool教程
采用spring框架写的一个简单的helloworld程序:
beans.xml配置
<bean id="helloword" class="com.tutorialspoint.HelloWorld"> <property name="message" value="Hello word111"></property> </bean>
HelloWorld实体类
public class HelloWorld { private String message; public String getMessage() { System.out.println("your mesage is:"+message); return message; } public void setMessage(String message) { this.message = message; } }
注册,加载,实例化,并应用bean
public static void main(String[] args) { //创建应用程序上下文环境,加载beans的配置,并初始化bean @SuppressWarnings("resource") ApplicationContext context=new ClassPathXmlApplicationContext("beans1.xml"); HelloWorld obj=(HelloWorld)context.getBean("helloword");//获取bean obj.getMessage();//调用bean的方法 }
学习教程:wscschool教程