首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

springMVC 浏览器直接展示jsp源码。求解

2013-10-12 
springMVC 浏览器直接显示jsp源码。求解?controller代码package com.controllerimport org.apache.log4j.L

springMVC 浏览器直接显示jsp源码。求解?

controller代码


package com.controller;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.User;
import com.service.IUserService;
import com.validator.UserValidator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private IUserService userService;
@Autowired
private UserValidator userValidator;

@RequestMapping(value = "/getUser/id/{userID}")
public ModelAndView getUser(@PathVariable int userID) throws Exception {
ModelAndView model=new ModelAndView("hello");
model.addObject("hello_1", userService.getUserByID(userID));
Logger logger = Logger.getLogger(UserController.class);
logger.info("test");
return model;
}

@RequestMapping(value="/listPageUser")
public ModelAndView listPageUser(User user){
ModelAndView model=new ModelAndView("hello_s");
model.addObject("hello_s", userService.listPageUser(user));
return model;
}

@RequestMapping(value = "/getStr")
@ResponseBody
public String getStr(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return "hello!";
}

@RequestMapping(value = "/getJSP")
public String getJSP(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("hello_1", userService.getUserByID(1));
return "hello";
}
@RequestMapping(value="/delete/id/{id}")
@ResponseBody
public String deleteUser(@PathVariable int id){
userService.deleteUserByID(id);
return "Success!";
}
@RequestMapping(value="/saveUser")
@ResponseBody
public String saveUser(User user,BindingResult result){
userValidator.validate(user,result);
if(result.hasErrors()){
return "fail!";
}
int userID=userService.saveUser(user);
return "userID="+userID;
}
@RequestMapping(value="/throwException")
public String throwException(){
int bbb=Integer.valueOf(null);
return "exception";
}
}


spring配置文件

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 连接池配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/myBatis?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- MyBatis的 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>


<property name="dataSource" ref="dataSource" />
</bean>
<!-- 以下是springMVC配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 使Spring关注Annotation -->

<context:annotation-config />

<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="com.controller" />
<context:component-scan base-package="com.dao" />
<context:component-scan base-package="com.service.impl" />
<context:component-scan base-package="com.validator" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 静态资源-->
<mvc:default-servlet-handler/> 
<!-- <mvc:resources mapping="/staticFile/**" location="/staticFile/"/>-->

<!-- 拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/getUser/**" /> 
<bean class="com.Interceptor.UserInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> 

    <!-- 总错误处理-->  
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="defaultErrorView">    
            <value>error/error</value>  
        </property>  
        <property name="defaultStatusCode">    
            <value>500</value>  
        </property>     
    <property name="warnLogCategory">    
            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>  
        </property>     
    </bean> 
    <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="query*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 配置参与事务的类或方法 -->
<aop:config>
<aop:pointcut id="allServiceMethod" expression="execution(* com.service.*.*.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
</aop:config>  


</beans>

spring?mvc?
[解决办法]
引用:
Quote: 引用:

ServerApache-Coyote/1.1

你的代码有部署到tomcat么?

是的,部署在myeclipse的默认tomcat下


但是响应头中看到的信息是apache返回的,可能是apache和tomcat整合有问题,也可能是apache中也部署了代码
建议你直接访问tomcat,不走apache试试

热点排行